📜  如何保存 Matplotlib 动画?

📅  最后修改于: 2022-05-13 01:55:30.783000             🧑  作者: Mango

如何保存 Matplotlib 动画?

在本文中,我们将学习如何保存 Matplotlib 动画。在 matplotlib 的帮助下制作的动画图形可以在Python保存为视频。因为我们可以使用 matplotlib 库创建迷人的动画。如果您想学习创建动画,这里是使用 matplotlib 创建动画的文章的链接。在本文中,我们将学习如何在 matplotlib 中保存动画。要保存动画,我们可以使用Animation.save()Animation.to_html5_video()

Animation.to_html5_video() 将动画作为 HTML5 视频标签返回。将动画保存为h264编码的视频,可以直接在notebook中显示。

示例 1:

Python3
# importing required libraries
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.animation as animation
from IPython import display
  
# initializing a figure
fig = plt.figure()
  
# labeling the x-axis and y-axis
axis = plt.axes(xlim=(0, 4),  ylim=(-1.5, 1.5))
  
# initializing a line variable
line, = axis.plot([], [], lw=3)
  
def animate(frame_number):
    x = np.linspace(0, 4, 1000)
  
    # plots a sine graph
    y = np.sin(2 * np.pi * (x - 0.01 * frame_number))
    line.set_data(x, y)
    line.set_color('green')
    return line,
  
  
anim = animation.FuncAnimation(fig, animate, frames=100, 
                               interval=20, blit=True)
fig.suptitle('Sine wave plot', fontsize=14)
  
# converting to an html5 video
video = anim.to_html5_video()
  
# embedding for the video
html = display.HTML(video)
  
# draw the animation
display.display(html)
plt.close()


Python3
# importing required libraries
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.animation as animation
from IPython import display
  
# initializing a figure
fig = plt.figure()
  
# labeling the x-axis and y-axis
axis = plt.axes(xlim=(0, 1000),  ylim=(0, 1000))
  
# lists storing x and y values
x, y = [], []
  
line, = axis.plot(0, 0)
  
  
def animate(frame_number):
    x.append(frame_number)
    y.append(frame_number)
    line.set_xdata(x)
    line.set_ydata(y)
    return line,
  
  
anim = animation.FuncAnimation(fig, animate, frames=1000, 
                               interval=20, blit=True)
fig.suptitle('Straight Line plot', fontsize=14)
  
# saving to m4 using ffmpeg writer
writervideo = animation.FFMpegWriter(fps=60)
anim.save('increasingStraightLine.mp4', writer=writervideo)
plt.close()


输出:



动画 matplotlib

为了使用 Animation.save() 方法保存动画,我们需要提供 writer 参数。以下是一些可用于编写动画的常见作家:

示例 2:

蟒蛇3

# importing required libraries
from matplotlib import pyplot as plt
import numpy as np
import matplotlib.animation as animation
from IPython import display
  
# initializing a figure
fig = plt.figure()
  
# labeling the x-axis and y-axis
axis = plt.axes(xlim=(0, 1000),  ylim=(0, 1000))
  
# lists storing x and y values
x, y = [], []
  
line, = axis.plot(0, 0)
  
  
def animate(frame_number):
    x.append(frame_number)
    y.append(frame_number)
    line.set_xdata(x)
    line.set_ydata(y)
    return line,
  
  
anim = animation.FuncAnimation(fig, animate, frames=1000, 
                               interval=20, blit=True)
fig.suptitle('Straight Line plot', fontsize=14)
  
# saving to m4 using ffmpeg writer
writervideo = animation.FFMpegWriter(fps=60)
anim.save('increasingStraightLine.mp4', writer=writervideo)
plt.close()

输出:

保存动画 matplotlib