Home  >  Article  >  Backend Development  >  How to draw a four-leaf clover using python

How to draw a four-leaf clover using python

藏色散人
藏色散人Original
2020-05-30 09:46:0910773browse

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.

How to draw a four-leaf clover using python

Create a new canvas and determine the canvas size:

plt.figure(figsize=(6,6))

How to draw a four-leaf clover using python

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

How to draw a four-leaf clover using python

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:])

How to draw a four-leaf clover using python

Convert polar coordinates to rectangular coordinates:

x=f(t)*np.cos(t)
y=f(t)*np.sin(t)

How to draw a four-leaf clover using python

##Draw the four-leaf rose line:

plt.plot(x,y,c='g')

How to draw a four-leaf clover using python

Color fill is green:

plt.fill(x,y,c='g')

How to draw a four-leaf clover using python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn