作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何将参数传递给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()
最佳答案
检查这个简单的例子:
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
data = np.loadtxt("example.txt", delimiter=",")
x = data[:,0]
y = data[:,1]
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([],[], '-')
line2, = ax.plot([],[],'--')
ax.set_xlim(np.min(x), np.max(x))
ax.set_ylim(np.min(y), np.max(y))
def animate(i,factor):
line.set_xdata(x[:i])
line.set_ydata(y[:i])
line2.set_xdata(x[:i])
line2.set_ydata(factor*y[:i])
return line,line2
K = 0.75 # any factor
ani = animation.FuncAnimation(fig, animate, frames=len(x), fargs=(K,),
interval=100, blit=True)
plt.show()
首先,对于数据处理推荐使用NumPy,最简单的读写数据。
您不必在每个动画步骤中使用“plot”函数,而是使用 set_xdata
和 set_ydata
方法来更新数据。
还回顾了 Matplotlib 文档的示例:http://matplotlib.org/1.4.1/examples/animation/ .
关于python - 如何将参数传递给 animation.FuncAnimation()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111571/
我是一名优秀的程序员,十分优秀!