Matplotlib 是一个非常广泛的库,它也支持图形动画。动画工具以 matplotlib.animation 基类为中心,它提供了一个框架,围绕该框架构建动画功能。主要接口有TimedAnimation和FuncAnimation,两者中FuncAnimation是最方便使用的。
1 画螺旋曲线代码
<span style="color:#333333"><span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#888888"># importing required modules</span>
<span style="color:#333333"><strong>import</strong></span> matplotlib.pyplot <span style="color:#333333"><strong>as</strong></span> plt
<span style="color:#333333"><strong>import</strong></span> matplotlib.animation <span style="color:#333333"><strong>as</strong></span> animation
<span style="color:#333333"><strong>import</strong></span> numpy <span style="color:#333333"><strong>as</strong></span> np
<span style="color:#888888"># create a figure, axis and plot element</span>
fig = plt.figure()
ax = plt.axes(xlim=(<span style="color:#880000">-50</span>, <span style="color:#880000">50</span>), ylim=(<span style="color:#880000">-50</span>, <span style="color:#880000">50</span>))
line, = ax.plot([], [], lw=<span style="color:#880000">2</span>)
<span style="color:#888888"># initialization function</span>
<span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>init</strong></span>():
<span style="color:#888888"># creating an empty plot/frame</span>
line.set_data([], [])
<span style="color:#333333"><strong>return&l