gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:43:56 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com