如何将参数传递给animation.FuncAnimation()
?我试过了,但没成功。animation.FuncAnimation()
的签名是
class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs) Bases: matplotlib.animation.TimedAnimation
我把代码贴在下面了。我要做哪些改变?
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def animate(i,argu):
print argu
graph_data = open('example.txt','r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
plt.grid()
ani = animation.FuncAnimation(fig,animate,fargs = 5,interval = 100)
plt.show()
检查这个简单的例子:
首先,对于数据处理建议使用NumPy,是最简单的读写数据。
不需要在每个动画步骤中使用“plot”函数,而是使用
set_xdata
和set_ydata
方法来更新数据。还回顾了Matplotlib文档的示例:http://matplotlib.org/1.4.1/examples/animation/。
我想你已经差不多到了,下面有一些小的调整,基本上你需要定义一个图形,使用轴句柄并把
fargs
放在一个列表中我将
example.txt
替换为一个硬连线字符串,因为我没有该文件,并且添加了对i
的依赖关系,所以绘图会移动。相关问题 更多 >
编程相关推荐