简体   繁体   English

matplotlib的动画不能在spyder中工作

[英]Animation from matplotlib not working in spyder

I'm new to both python and stackoverflow, and I'm going over examples at matplotlib. 我是python和stackoverflow的新手,我正在matplotlib上看一些例子。 I've searched for the solution to this problem with no luck, although I was able to locate a previously unanswered question in stackoverflow with the same issue. 我没有运气地搜索了这个问题的解决方案,虽然我能够在stackoverflow中找到一个以前未回答的问题 ,同样的问题。

Basically, I copied the code available from the examples at matplotlib ; 基本上,我复制了matplotlib中示例中提供的代码; for instance: 例如:

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()

I've run various animation examples in both Anaconda 2 (python 2.7) & 3 (python 3.5), and both give me a blank plot without animation. 我在Anaconda 2(python 2.7)和3(python 3.5)中都运行了各种动画示例,两者都给了我一个没有动画的空白图。 However, each animation works perfectly well in Enthought Canopy. 但是,每个动画在Enthought Canopy中运行得非常好。

Is there something simple I'm missing when using spyder? 使用Spyder时有什么简单的东西吗?

You have to change the backend to run an animation in the IPython console. 您必须更改后端才能在IPython控制台中运行动画。 You can do that by running %matplotlib qt command before the animation. 你可以通过在动画之前运行%matplotlib qt命令来做到这一点。

If You don't want to use this command every time, You can go to: Tools > Preferences > IPython Console > Graphics > Backend and change it from "Inline" to "Automatic" . 如果您不想每次都使用此命令,可以转到: Tools > Preferences > IPython Console > Graphics > Backend ,并将其从"Inline"更改为"Automatic"

Update: Feb 2018, this is now in python>Preferences In the window select IPython console in the LH pane of the window. 更新:2018年2月,现在是python> Preferences在窗口中,在窗口的LH窗格中选择IPython console。 Select the Graphics tab and backend is in there. 选择Graphics选项卡,后端就在那里。

For more details please read this . 有关详细信息,请阅读此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM