来自matplotlib的动画在spyd中不工作

2024-04-29 15:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和stackoverflow都是新手,我将介绍matplotlib中的示例。虽然我在stackoverflow中找到了一个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(python2.7)&3(python 3.5)中运行了各种动画示例,它们都给了我一个没有动画的空白图。然而,每一个动画都在热情的树冠下完美地工作。

在使用spyder的时候,我有没有遗漏一些简单的东西?


Tags: import示例datamatplotlibdefasnpline
1条回答
网友
1楼 · 发布于 2024-04-29 15:35:29

必须更改后端才能在IPython控制台中运行动画。可以在动画之前运行%matplotlib qt命令。

如果不想每次都使用此命令,可以转到: Tools > Preferences > IPython Console > Graphics > Backend 把它从"Inline"改成"Automatic"

更新:2018年2月,现在在python>;首选项中,在窗口的左侧窗格中选择IPython控制台。选择图形选项卡,后端在其中。

有关详细信息,请阅读this

相关问题 更多 >