来自matplotlib的动画无法在spyder中使用 [英] Animation from matplotlib not working in spyder

查看:108
本文介绍了来自matplotlib的动画无法在spyder中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python和stackoverflow的新手,并且我将讨论matplotlib上的示例。尽管可以找到先前未得到解答的问题,但我一直没有运气寻找此问题的解决方案在同一问题中。

I'm new to both python and stackoverflow, and I'm going over examples at 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.

基本上,我从 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(蟒蛇2 .7)& 3(python 3.5),两者都给了我一个没有动画的空白情节。但是,每个动画在有思想的树冠中都能完美地运行。

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. However, each animation works perfectly well in Enthought Canopy.

使用spyder时是否缺少简单的东西?

Is there something simple I'm missing when using spyder?

推荐答案

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

You have to change the backend to run an animation in the IPython console. You can do that by running %matplotlib qt command before the animation.

如果您不想每次都使用此命令,则可以转到:
工具>偏好设置> IPython控制台>图形>后端
,并将其从 Inline 更改为 Automatic

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".

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

Update: Feb 2018, this is now in python>Preferences In the window select IPython console in the LH pane of the window. Select the Graphics tab and backend is in there.

有关更多详细信息,请阅读

For more details please read this.

这篇关于来自matplotlib的动画无法在spyder中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆