G. *_*cia 4 html python 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([],[])
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()
Run Code Online (Sandbox Code Playgroud)
我基本上做的是将生成的代码复制到一个基本的 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>
Run Code Online (Sandbox Code Playgroud)
图表未显示,但标题和视频菜单在那里。为什么?我该如何解决?
只是陈述ani.to_html5_video()不会给你正确的字符串。如果您仔细查看您复制的内容,您会\n在输出中看到换行符 ( )。(我在截图中标记了它们......)
用
print(ani.to_html5_video())
Run Code Online (Sandbox Code Playgroud)
获取视频的HTML代码。或者,直接保存到文件
with open("myvideo.html", "w") as f:
print(ani.to_html5_video(), file=f)
Run Code Online (Sandbox Code Playgroud)
作为一个解释性的例子,取
string = "Hello\nWorld"
Run Code Online (Sandbox Code Playgroud)
如果你string在单元格中声明它会产生
'Hello\nWorld'
Run Code Online (Sandbox Code Playgroud)
但是如果你print(string),它会给你解释的字符串
Hello
World
Run Code Online (Sandbox Code Playgroud)