【问题标题】:I've got some problems of iteration with my animation function (matplotlib.animation/Python)我的动画函数(matplotlib.animation/Python)有一些迭代问题
【发布时间】:2020-04-10 19:54:20
【问题描述】:

我正在尝试使用 matplotlib.animation 创建一个动画直方图,但 animation.FuncAnimation 无法正常运行:使用此代码时,我在官方文档中找到,

"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))


def animate(i):
    print(i)
    line.set_ydata(np.sin(x + i/10.0))  # update the data
    return line,


# Init only required for blitting to give a clean slate.
def init():
     line.set_ydata(np.ma.array(x, mask=True))
     return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200),init_func=init,interval=25, blit=True)
plt.show()

我得到的最终结果是由 init() 函数创建的图(也是一个空图),但没有动画迭代。此外,我测试了其他代码,实际上给了我相同的结果:我得到了初始化,或第一帧,但没有更多。 matplotlib 和 matplotlib.animation 已安装,一切似乎都很好,除了它不起作用。有人知道如何解决它吗? (提前谢谢你:)!)

【问题讨论】:

  • 您在哪里以及如何运行它?你有哪些版本的python、matplotlib?
  • 对不起,我刚刚看到你的评论 x) !我在我的 IPython 控制台中使用 Spyder 在 Python 3.6 下运行我的程序,我的 matplotlib 版本是 2.0.2!

标签: python matplotlib


【解决方案1】:

我在使用 Jupyter 笔记本时遇到了同样的问题,我通过插入行解决了它

%matplotlib notebook

在代码中。

【讨论】:

    【解决方案2】:

    可能是您的 Spyder 中的 IPython 配置为自动使用内联后端。这会将控制台内的绘图显示为 png 图像。当然 png 图像不能被动画化。

    我建议不要使用 IPython,而是在专用的 Python 控制台中执行脚本。在 Spyder 中,转到 Run/Configure.. 并将选项设置为新的专用 Python 控制台。

    【讨论】:

    • 完美!非常感谢您的快速回答:)!
    猜你喜欢
    • 2015-04-29
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多