Python 如何将matplotlib动画转换为HTML5<;视频>;标签

Python 如何将matplotlib动画转换为HTML5<;视频>;标签,python,html,matplotlib,Python,Html,Matplotlib,下面是matplolib动画图的代码,以及如何保存它 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([],

下面是matplolib动画图的代码,以及如何保存它

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>

页面标题
我的第一个标题
布拉布拉

图形未显示,但标题和视频菜单在那里。为什么?我怎样才能修好它

to_html5函数的默认
embed_限制为20 MB

If the embed_limit is exceeded, this returns the string "Video too large to embed."
我猜你的动画超过了这个限制。在HTML中定义
标记时,仍然会生成标题和控件


您可以查看生成的文档,看看视频主体是否只是说“视频太大,无法嵌入”。如果是这种情况,您可以尝试增加
嵌入\u限制

,仅说明
ani。to_html5\u video()
不会给出正确的字符串。 如果仔细查看复制的内容,您将看到输出中到处都是换行符(
\n
)。(我在截图中标记了它们…)

使用

获取视频的HTML代码。或者,直接保存到文件中

with open("myvideo.html", "w") as f:
    print(ani.to_html5_video(), file=f)

解释

作为一个解释性的例子,以

string = "Hello\nWorld"
如果在单元格中声明
string
,它将生成

'Hello\nWorld'
但是如果您打印(字符串)
,它将为您提供解释后的字符串

Hello
World

我认为这个问题缺少了非常重要的一步,即ani.to_html5_video()与
BLABLA
的关系。可能你只是在复制一个到另一个时犯了一个错误?嘿,我得到的链接太长了,所以我不想复制粘贴它,我找不到附加文件的方法。以下是我的驱动器中的链接:
Hello
World