【问题标题】:How is the function argument defined in matplotlib animation?matplotlib动画中的函数参数是如何定义的?
【发布时间】:2016-11-26 15:08:15
【问题描述】:

大家好,我是 python/matplotlib 的新手。

我很难理解 matplotlib 网站上的以下动画代码。如果在 中调用此函数,def update 中的 data 参数如何定义animation.FuncAnimation 没有指定任何 data 作为输入参数?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)


def update(data):
    line.set_ydata(data)
    return line,


def data_gen():
    while True:
        yield np.random.rand(10)

ani = animation.FuncAnimation(fig, update, data_gen, interval=100)
plt.show()

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    data_gen 是一个生成器,它为每个动画帧生成一个包含 10 个随机值的数组。从data_gen 返回的数组是传递给update 函数的第一个输入参数。

    如果您使用fargs kwarg 调用FuncAnimation,您还可以将其他 输入传递给update 函数。

    def update(data, scale):
        line.set_ydata(scale * data)
        return line
    
    animation.FuncAnimation(fig, update, data_gen, fargs=(100,), interval=100)
    

    【讨论】:

    • 谢谢 Suever,我想知道...有没有办法以更简单的方式生成动画?当你连续调用 plot(x,y) 时,就像在 GNU octave 中一样?
    • @PintoDoido 是的,你可以。 here is a post 有一些方法可以做到这一点
    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2018-07-20
    • 2017-07-13
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多