Python Matplotlib动画-如何将其导出为在演示文稿中使用的格式?
所以,我学会了如何在matplotlib中制作可爱的小动画。例如,这:Python Matplotlib动画-如何将其导出为在演示文稿中使用的格式?,python,animation,matplotlib,Python,Animation,Matplotlib,所以,我学会了如何在matplotlib中制作可爱的小动画。例如,这: import numpy as np import matplotlib import matplotlib.pyplot as plt plt.ion() fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlim(0, 1) ax.set_ylim(-2,2) dt = 0.01 q = 0.01 t = np.arange(0,1,dt) x = n
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(0, 1)
ax.set_ylim(-2,2)
dt = 0.01
q = 0.01
t = np.arange(0,1,dt)
x = np.sin(2*np.pi*t)
line, = ax.plot(t,x, '-')
fig.canvas.draw()
for i in xrange(100):
x = (1-q) * x + q* np.random.normal(size = len(t))
line.set_ydata(x)
fig.canvas.draw()
这很管用,而且非常好。但是我如何使用它来制作一部可以在pdf演示文稿中显示的电影呢?我试图执行fig.savefig(“test.gif”)
,但出现一条错误消息,表明matplotlib不导出gif
有没有一种方法可以不用借助外部工具(比如制作大量png并将它们缝合在一起)?简而言之,没有,除了使用外部工具之外,没有其他方法。在1.1.0中有一个新模块,可以帮助您完成并将其保存为mpeg4格式。它使用外部工具自动执行此转换,从多帧转换为单个电影。您可以使用新动画模块提供的两个选项“转换”或“使用或”生成gif。如果您使用
matplotlib.animation
并在路径中包含类似于FFmpeg
的内容,则这应该可以工作:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(0, 1)
ax.set_ylim(-2,2)
dt = 0.01
q = 0.01
t = np.arange(0,1,dt)
x = np.sin(2*np.pi*t)
images = []
for i in xrange(100):
x = (1-q) * x + q* np.random.normal(size = len(t))
line, = ax.plot(t,x, '-')
images.append((line,))
line_anim = ArtistAnimation(fig, images, interval=50, blit=True)
line_anim.save('my_animation.mp4')
plt.show()
可爱,嗯?+1。但是,您可能指的是mencoder而不是memcoder,对吗?对,所以我只添加了一个
fig.savefig(“foo%d.png”%I)
,以保存每个帧并准确地使用mencoder。这就是新的动画模块所做的。如果您不想使用该模块,请从python中调用mencoder,然后您可以在之后取消foo*png文件的链接。的确!有没有办法设置输出mp4的帧速率@DanoDonova如果更改ArtistAnimation
中的interval
参数,则应更改帧率。可能还有一个FFmpeg
变量,但我不是FFmpeg
专家。谢谢!这很有帮助。