Matplotlib 动画在 IPython Notebook 中不起作用(空白图) [英] Matplotlib animation not working in IPython Notebook (blank plot)

查看:29
本文介绍了Matplotlib 动画在 IPython Notebook 中不起作用(空白图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了多个动画示例代码,但无法让其中任何一个正常工作.这是我从 Matplotlib 文档中尝试过的一个基本方法:

I've tried multiple animation sample codes and cannot get any of them working. Here's a basic one I've tried from the Matplotlib documentation:

"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)        # x-array
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/10.0))  # update the data
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
    interval=25, blit=True)
plt.show()

当我在 IPython Notebook 中执行上述操作时,我只看到生成了一个空白图.我试过在多台机器上,使用多种浏览器(Chrome、FF、IE)从多台服务器(包括 Wakari)运行它.

When I execute the above in an IPython Notebook, I just see a blank plot generated. I've tried running this from multiple servers (including Wakari), on multiple machines, using multiple browsers (Chrome, FF, IE).

我可以将动画保存为 mp4 文件就好了,播放时看起来不错.

I can save the animation to an mp4 file just fine and it looks good when played.

感谢任何帮助!

推荐答案

总结您拥有的选项:

  • 在循环中使用display 使用IPython.display.display(fig) 在输出中显示一个图形.使用循环,您可能希望在显示新图形之前清除输出.请注意,此技术通常提供不那么平滑的结果.因此,我建议使用以下任何一种.

  • Using display in a loop Use IPython.display.display(fig) to display a figure in the output. Using a loop you would want to clear the output before a new figure is shown. Note that this technique gives in general not so smooth resluts. I would hence advice to use any of the below.

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
from IPython.display import display, clear_output

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda i: l.set_data(t[:i], x[:i])

for i in range(len(x)):
animate(i)
clear_output(wait=True)
display(fig)

plt.show()

%matplotlib notebook 使用 IPython magic %matplotlib notebook 将后端设置为 notebook 后端.这将使图形保持活动状态而不是显示静态 png 文件,因此也可以显示动画.
完整示例:

%matplotlib notebook Use IPython magic %matplotlib notebook to set the backend to the notebook backend. This will keep the figure alive instead of displaying a static png file and can hence also show animations.
Complete example:

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

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda i: l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))

plt.show()

%matplotlib tk 使用IPython magic %matplotlib tk 将后端设置为tk 后端.这将在一个新的绘图窗口中打开图形,该窗口是交互式的,因此也可以显示动画.
完整示例:

%matplotlib tk Use IPython magic %matplotlib tk to set the backend to the tk backend. This will open the figure in a new plotting window, which is interactive and can thus also show animations.
Complete example:

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

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda i: l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))

plt.show()

将动画转换为 mp4 视频:

from IPython.display import HTML
HTML(ani.to_html5_video())

或在笔记本的开头使用 plt.rcParams["animation.html"] = "html5".这将需要有 ffmpeg 视频编解码器可用于转换为 HTML5 视频.然后视频内嵌显示.因此,这与 %matplotlib inline 后端兼容.完整示例:

or use plt.rcParams["animation.html"] = "html5" at the beginning of the notebook. This will require to have ffmpeg video codecs available to convert to HTML5 video. The video is then shown inline. This is therefore compatible with %matplotlib inline backend. Complete example:

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["animation.html"] = "html5"
import matplotlib.animation
import numpy as np

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda i: l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))
ani

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

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda i: l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))

from IPython.display import HTML
HTML(ani.to_html5_video())

将动画转换为 JavaScript:

from IPython.display import HTML
HTML(ani.to_jshtml())

或在笔记本的开头使用 plt.rcParams["animation.html"] = "jshtml".这将使用 JavaScript 将动画显示为 HTML.这与大多数新浏览器以及 %matplotlib inline 后端高度兼容.它在 matplotlib 2.1 或更高版本中可用.
完整示例:

or use plt.rcParams["animation.html"] = "jshtml" at the beginning of the notebook. This will display the animation as HTML with JavaScript. This highly compatible with most new browsers and also with the %matplotlib inline backend. It is available in matplotlib 2.1 or higher.
Complete example:

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["animation.html"] = "jshtml"
import matplotlib.animation
import numpy as np

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda i: l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))
ani

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

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda i: l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))

from IPython.display import HTML
HTML(ani.to_jshtml())

这篇关于Matplotlib 动画在 IPython Notebook 中不起作用(空白图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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