多语言展示
当前在线:1523今日阅读:91今日分享:37

python 如何画一条阿基米德螺线?

阿基米德螺线有着优秀的性质,而且在实际生活中有着广泛的应用,它的极坐标方程是:
工具/原料

Anaconda3.exe

方法/步骤
1

首先引入需要的python 模块:

2

其次设置figure的坐标系为极坐标,并设置半径的范围:

3

然后填充显示阿基米德螺线所需要的数据:

4

最后发送显示命令,准备显示:

5

得到最终的示意图:

6

让我们通过动画来看一下阿基米德螺线的运动轨迹:

源码:
1

简单版本:import matplotlib.pyplot as pltimport numpy as npfrom matplotlib import animationplt.subplot(111, polar=True)plt.ylim([0, 30])N = 4theta = np.arange(0, N * np.pi, np.pi / 100)plt.plot(theta, theta*2, '--')plt.show()

2

带动画的阿基米德螺线:import matplotlib.pyplot as pltimport numpy as npfrom matplotlib import animationfig = plt.figure()plt.subplot(111, polar=True)plt.ylim([0, 30])line, = plt.plot([], [], '--')def init():    line.set_data([], [])    return line,def animate(i):    if i!=0:        theta = np.arange(0, i * np.pi, np.pi / 100)        line.set_data(theta, theta*3)    return line,anim = animation.FuncAnimation(fig, animate, init_func=init,                                 frames=8, interval=300, blit=True) plt.show()

推荐信息