iPython + pylab/matplotlib animations hang

廉价感情. 提交于 2019-12-11 11:17:43

问题


I cannot for the life of me get iPython to show an animated plot. This code is taken directly from the matplotlib animation examples, and it works fine using plain old python (aka python animate.py).

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)

def update(data):
    line.set_ydata(data)
    return line,

def data_gen():
    while True: yield np.random.rand(10)

ani = animation.FuncAnimation(fig, update, data_gen, interval=100)

#plt.ion()
plt.show()

#while 1:
    #plt.draw()

calling with ipython --pylab auto animate.py, ipython --pylab tk animate.py both cause the window to disappear immediately. If I uncomment the ion() and plt.draw() it just draws a single frame and then hangs. I need to use iPython because it is much more thread-safe than the normal matplotlib implementation.

What am I doing wrong? Using python2.7.2 on osx 10.8 with iPython 1.1 and matplotlib 1.3


回答1:


The above - without uncommenting anythin works fine with just:

ipython animate.py

So does

ipython --pylab=auto

In [1]: import animate

So I think that your problem is that you are trying to combine interactive mode where the backend selection is important with a script that runs continuously with no interaction - this is a conflict.

Reading the documents here and here I suspect you need to make a call to IPython.lib.inputhook.InputHookManager in animate.py to set your backend.

IPython 0.13.2 matplotlib '1.3.0' kubuntu 13.10




回答2:


I too have been trying this. It seems the majority of users use JSAnimation from Jake Vanderplaas. Link. https://github.com/jakevdp/JSAnimation. This is not a package in the Enthought distribution, however, which then presents the need to import separately.



来源:https://stackoverflow.com/questions/19466825/ipython-pylab-matplotlib-animations-hang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!