Home>Article>Backend Development> How to draw a four-leaf clover using python
How to draw a four-leaf clover using python?
Open the spyder compiler and load the module first:
Recommended: "python tutorial"
import numpy as np import matplotlib.pyplot as plt
Used numpy and matplotlib two modules.
Create a new canvas and determine the canvas size:
plt.figure(figsize=(6,6))
The polar coordinate equation of the four-leaf clover is:
1 + cos(4*t) + 2 * (sin(4*t)) ^ 2
For this purpose, define a function:
def f(t): return 1+np.cos(4*t) + 2*(np.sin(4*t))**2
The value range of parameter t is 0 to 2π, subdivided into 1000 parts:
t= np.linspace(0, 2*np.pi, 1000) print(t[-20:])
Convert polar coordinates to rectangular coordinates:
x=f(t)*np.cos(t) y=f(t)*np.sin(t)##Draw the four-leaf rose line:
plt.plot(x,y,c='g')Color fill is green:
plt.fill(x,y,c='g')
The above is the detailed content of How to draw a four-leaf clover using python. For more information, please follow other related articles on the PHP Chinese website!