gpt4 book ai didi

python - 如何将参数传递给 animation.FuncAnimation()?

转载 作者:太空狗 更新时间:2023-10-30 01:46:13 25 4
gpt4 key购买 nike

如何将参数传递给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_xdataset_ydata 方法来更新数据。

还回顾了 Matplotlib 文档的示例:http://matplotlib.org/1.4.1/examples/animation/ .

关于python - 如何将参数传递给 animation.FuncAnimation()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com