如何将matplotlib动画转换为HTML5< video>标签 [英] How to convert matplotlib animation to an HTML5 <video> tag
问题描述
下面是matplolib动画图形的代码,并在
使用
print(ani.to_html5_video())
获取视频的HTML代码.或者,直接保存到文件
,其中open("myvideo.html","w")为f:打印(ani.to_html5_video(),file = f)
说明
作为示例,请
string ="Hello \ nWorld"
如果在单元格中声明 string
,它将产生
'Hello \ nWorld'
但是如果您 print(string)
,它将为您提供解释后的字符串
你好世界
Below there is the code of a matplolib animation graph and here is how to save it.
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()
What I basically do is copying the generated code into a basic html script like this one:
<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>
Graph is not showing but the heading and the video menu is there. Why? How can I fix it?
Just stating ani.to_html5_video()
will not give you the correct string out.
If you look closely at what you have copied, you will see linebreaks (\n
) all over the output. (I marked them in the screenshot...)
Use
print(ani.to_html5_video())
to obtain the HTML code of the video. Or, to directly save to a file
with open("myvideo.html", "w") as f:
print(ani.to_html5_video(), file=f)
Explanation
As an explanatory example, take
string = "Hello\nWorld"
If you state string
in the cell it will produce
'Hello\nWorld'
But if you print(string)
, it will give you the interpreted string
Hello
World
这篇关于如何将matplotlib动画转换为HTML5< video>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!