python - 来自 matplotlib 的动画在 spyder 中不起作用

标签 python animation matplotlib

我是 python 和 stackoverflow 的新手,我正在研究 matplotlib 中的示例。尽管我能够找到 previously unanswered question,但我已经搜索了这个问题的解决方案,但没有成功。在具有相同问题的计算器中。

基本上,我复制了示例中可用的代码,网址为 matplotlib。 ;例如:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def data_gen(t=0):
    cnt = 0
    while cnt < 1000:
        cnt += 1
        t += 0.1
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


def run(data):
    # update the data
    t, y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
                          repeat=False, init_func=init)
plt.show()

我在 Anaconda 2 (python 2.7) 和 3 (python 3.5) 中运行了各种动画示例,并且都给我一个没有动画的空白图。但是,每个动画在 Enthought Canopy 中都能完美运行。

我在使用 spyder 时是否遗漏了一些简单的东西?

最佳答案

您必须更改后端才能在 IPython 控制台中运行动画。您可以通过在动画之前运行 %matplotlib qt 命令来做到这一点。

如果你不想每次都使用这个命令,你可以去: 工具 > 首选项 > IPython 控制台 > 图形 > 后端 并将其从 "Inline" 更改为 "Automatic"

更新:2018 年 2 月,现在在 python>Preferences 窗口中,在窗口的 LH Pane 中选择 IPython console。选择“图形”选项卡,后端就在那里。

更多详情请阅读this .

关于python - 来自 matplotlib 的动画在 spyder 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35856079/

相关文章:

python - 用\and 分隔的十六进制数据是什么类型的?

python - Numpy 过滤器像素索引

python - 使用 python 中的 PyQt5 按下按钮时更改 QML 中的标签

animation - CSS3 链式动画

python - 使用或不使用颜色条对齐 matplotlib 子图轴(对于 2 个以上的轴)

python - 在Python的Matplotlib中,如何让薄图像与主图像边框?

python - Django,首先继承 models.Model 还是首先继承非模型类?

css - 如何使用剪辑路径和 dashoffset 为自定义 SVG 设置动画?

ios - 快速动画旋转标签?

python - 为什么 matplotlib 对于 'making' 绘图有这么多选项?