Python FuncAnimation无法识别更新(Python FuncAnimation not recognizing update)

编程入门 行业动态 更新时间:2024-10-25 12:24:59
Python FuncAnimation无法识别更新(Python FuncAnimation not recognizing update)

我正在学习如何在python中为我的一个项目制作动画,并且我将我的代码从以下示例中删除 。

我对其代码的调整如下:

import numpy as np import h5py, os, glob, sys, time import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def update(i): for j in np.arange(0,10): for k in np.arange(0,10): for channel in ["N","E"]: x = some_x_value y = some_y_value line = plt.loglog(x,y) ax.set_xlabel(label) return line, ax if __name__ == "__main__": fig, ax = plt.subplots() anim = FuncAnimation(fig, update, frames=np.arange(0,10), interval=200) anim.save('Test.gif', dpi=80, writer='imagemagick')

当我尝试运行我的脚本时,我收到以下错误:名称错误:未定义名称“更新”。

正如我之前所说,我仍然在学习如何制作动画并且不理解我发现的代码教程中发生的所有事情。 但是,我很困惑为什么更新根本不被识别,因为我调用update的方式似乎与教程中的内容完全相同。

I'm learning how to animate in python for one of my projects and I'm basing my code off of the following example from here.

My adaption of their code goes as follows:

import numpy as np import h5py, os, glob, sys, time import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def update(i): for j in np.arange(0,10): for k in np.arange(0,10): for channel in ["N","E"]: x = some_x_value y = some_y_value line = plt.loglog(x,y) ax.set_xlabel(label) return line, ax if __name__ == "__main__": fig, ax = plt.subplots() anim = FuncAnimation(fig, update, frames=np.arange(0,10), interval=200) anim.save('Test.gif', dpi=80, writer='imagemagick')

And when I try to run my script I get the following error: Name Error: name 'update' is not defined.

As I said before, I'm still learning how to animate and don't understand all of what's going on in the code tutorial I found. However, I'm very confused as to why update isn't recognized at all as the way I call update seems to be exactly the same as what's in the tutorial.

最满意答案

import numpy as np import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def update(i, ln): i = i+1 x = i y = i ** 2 x_data = ln.get_xdata() y_data = ln.get_ydata() ln.set_data(np.concatenate(([x], x_data)), np.concatenate(([y], y_data))) return ln if __name__ == "__main__": fig, ax = plt.subplots() ax.set_xlim(1, 10) ax.set_ylim(1, 100) line, = ax.loglog([1], [1]) anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200, fargs=(line, )) anim.save('Test.gif', dpi=80, writer='imagemagick')

按预期工作。 这让我觉得你的代码中还有一些其他错误被掩盖了。

import numpy as np import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def update(i, ln): i = i+1 x = i y = i ** 2 x_data = ln.get_xdata() y_data = ln.get_ydata() ln.set_data(np.concatenate(([x], x_data)), np.concatenate(([y], y_data))) return ln if __name__ == "__main__": fig, ax = plt.subplots() ax.set_xlim(1, 10) ax.set_ylim(1, 100) line, = ax.loglog([1], [1]) anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200, fargs=(line, )) anim.save('Test.gif', dpi=80, writer='imagemagick')

Works as expected. This makes me think that there is some other error in your code which is getting masked.

更多推荐

本文发布于:2023-07-20 20:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法识别   FuncAnimation   Python   recognizing   update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!