Python怎么画四叶草?Python画四叶草方法
时间:2022-10-26 22:19
今天小编为大家带来的是Python画四叶草方法,想知道怎么做的小伙伴来看看接下来的这篇文章吧,相信一定会帮到你们的。
Python怎么画四叶草?Python画四叶草方法
1、首先打开spyder编译器,先加载模块:import numpy as np,import matplotlib.pyplot as plt,用到了numpy和matplotlib两个模块。

2、然后新建画布,并确定画布大小:plt.figure(figsize=(6,6))。

3、四叶草的极坐标方程是:1 + cos(4*t) + 2 * (sin(4*t)) ^ 2,为此,自定义一个函数:def f(t):return 1+np.cos(4*t) + 2*(np.sin(4*t))**2。

4、参数t的取值范围是0到2π,细分为1000份:t= np.linspace(0, 2*np.pi, 1000)print(t[-20:])。

5、接着把极坐标转化为直角坐标:x=f(t)*np.cos(t) y=f(t)*np.sin(t)。

6、再绘制四叶玫瑰线:plt.plot(x,y,c='g')。

7、最后颜色填充为绿色:plt.fill(x,y,c='g')。

谢谢大家阅读观看,希望本文能帮到大家!