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

How to draw a four-leaf clover using python

藏色散人
Release: 2020-05-30 09:46:09
Original
10881 people have browsed it

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
Copy after login

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))
Copy after login

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
Copy after login

For this purpose, define a function:

def f(t):
    return 1+np.cos(4*t) + 2*(np.sin(4*t))**2
Copy after login

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:])
Copy after login

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)
Copy after login

How to draw a four-leaf clover using python

##Draw the four-leaf rose line:

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

How to draw a four-leaf clover using python

Color fill is green:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template