下面是matplotlib动画图的代码,here是如何保存它。
from IPython.display import HTML
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()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])
def animate(i):
l.set_data(t[:i], x[:i]);
ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t));
#HTML(ani.to_jshtml())
ani.to_html5_video()
我基本上做的是将生成的代码复制到像这样的基本html脚本中:
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<video width="432" height="288" controls autoplay loop> BLABLA </video>
</body>
</html>
图表没有显示,但标题和视频菜单就在那里。为什么?我该如何解决?
只是说明ani.to_html5_video()
不会给你正确的字符串。如果仔细查看已复制的内容,您将在输出中看到换行符(\n
)。 (我在截图中标记了它们......)
使用
print(ani.to_html5_video())
获取视频的HTML代码。或者,直接保存到文件
with open("myvideo.html", "w") as f:
print(ani.to_html5_video(), file=f)
作为一个解释性的例子,拿
string = "Hello\nWorld"
如果你在单元格中陈述string
它会产生
'Hello\nWorld'
但如果你print(string)
,它会给你解释的字符串
Hello
World
to_html5
函数的默认embed_limit
为20 MB。
If the embed_limit is exceeded, this returns the string "Video too large to embed."
我的猜测是你的动画超过了这个限制。当您在HTML中定义<video>
标记时,仍会生成标题和控件。
您可以查看生成的文档,看看视频正文是否只是“视频太大而无法嵌入”。如果是这种情况,您可以尝试增加embed_limit
。