我是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(python 2.7)和3(python 3.5)中都运行了各种动画示例,两者都给了我一个没有动画的空白图。但是,每个动画在Enthought Canopy中运行得非常好。
使用Spyder时有什么简单的东西吗?
您必须更改后端才能在IPython控制台中运行动画。你可以通过在动画之前运行%matplotlib qt
命令来做到这一点。
如果您不想每次都使用此命令,您可以访问:Tools > Preferences > IPython Console > Graphics > Backend
并将其从"Inline"
更改为"Automatic"
。
更新:2018年2月,现在是python> Preferences在窗口中,在窗口的LH窗格中选择IPython console。选择Graphics选项卡,后端就在那里。
有关更多详细信息,请阅读this。