【问题标题】:Is there Matplotlib FuncAnimation not work now?现在有 Matplotlib FuncAnimation 不工作吗?
【发布时间】:2021-02-27 11:09:54
【问题描述】:

我目前尝试使用 matplotlib.animation.FuncAnimation 来创建动态烛台,但是当我使用像这样的示例代码 link.

我的python版本是3.7.1

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

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return ln,

def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)
plt.show()

但作为图像,我得到一个空的数字。 enter image description here

我不知道发生了什么。

所以我来这里寻求你的帮助。

你们所有的回答对我很有帮助,意义重大。 谢谢。

【问题讨论】:

  • Jupyter 中的绘图采用 png 格式。所以它不会被动画化。看看this answer。您的代码在 Python 解释器终端中运行良好。
  • @funie200 有点奇怪,我记得以前在 jupyter notebook 里画过 gif,我觉得我脑子有问题,非常感谢。

标签: python python-3.x matplotlib animation jupyter-notebook


【解决方案1】:

我之前找到了可以在jupyter-notebook中运行Animation的原因。

我使用了 ipywidgets 模块中的交互功能。

而且这个功能需要使用交互式后端; %matplotlib notebook

不知何故,我打开了我的旧动画代码,但 %matplotlib notebook 不见了。

所以我无法运行我的动画。

在图像中会是这样的结果。

再次感谢 StackOverflow。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多