如何将参数传递给animation.FuncAnimation()? [英] How to pass arguments to animation.FuncAnimation()?

查看:19
本文介绍了如何将参数传递给animation.FuncAnimation()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将参数传递给 animation.FuncAnimation()?我试过了,但是没用. animation.FuncAnimation()的签名为

How to pass arguments to animation.FuncAnimation()? I tried, but didn't work. The signature of animation.FuncAnimation() is

matplotlib.animation.FuncAnimation类(无花果,func,帧=无,init_func =无,fargs =无,save_count =无,** kwargs)基数:matplotlib.animation.TimedAnimation

class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs) Bases: matplotlib.animation.TimedAnimation

我在下面粘贴了我的代码.我必须做出哪些改变?

I have pasted my code below. Which changes I have to make?

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,这是最简单的读写数据.

First, for data handling is recommended to use NumPy, is simplest read and write data.

不必在每个动画步骤中都使用绘图"功能,而是使用 set_xdata set_ydata 方法更新数据.

Isn't necessary that you use the "plot" function in each animation step, instead use the set_xdata and set_ydata methods for update data.

还要查看Matplotlib文档的示例: http://matplotlib.org/1.4.1/examples/animation/.

Also reviews examples of the Matplotlib documentation: http://matplotlib.org/1.4.1/examples/animation/.

这篇关于如何将参数传递给animation.FuncAnimation()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆