📜  Python中的 Matplotlib.animation.FuncAnimation 类

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

Python中的 Matplotlib.animation.FuncAnimation 类

Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。

matplotlib.animation.FuncAnimation

matplotlib.animation.FuncAnimation类用于通过重复调用相同的函数(即func)来制作动画。

类的方法:

MethodsDescription
__init__(self, fig, func[, frames, …])Initialize self.
new_frame_seq(self)Return a new sequence of frame information.
new_saved_frame_seq(self)Return a new sequence of saved/cached frame information.
save(self, filename[, writer, fps, dpi, …])Save the animation as a movie file by drawing every frame.
to_html5_video(self[, embed_limit])Convert the animation to an HTML5
to_jshtml(self[, fps, embed_frames, …])Generate HTML representation of the animation

示例 1:

Python3
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
 
 
# creating a blank window
# for the animation
fig = plt.figure()
axis = plt.axes(xlim =(-50, 50),
                ylim =(-50, 50))
 
line, = axis.plot([], [], lw = 2)
 
# what will our line dataset
# contain?
def init():
    line.set_data([], [])
    return line,
 
# initializing empty values
# for x and y co-ordinates
xdata, ydata = [], []
 
# animation function
def animate(i):
    # t is a parameter which varies
    # with the frame number
    t = 0.1 * i
     
    # x, y values to be plotted
    x = t * np.sin(t)
    y = t * np.cos(t)
     
    # appending values to the previously
    # empty x and y data holders
    xdata.append(x)
    ydata.append(y)
    line.set_data(xdata, ydata)
     
    return line,
 
# calling the animation function    
anim = animation.FuncAnimation(fig, animate,
                            init_func = init,
                            frames = 500,
                            interval = 20,
                            blit = True)
 
# saves the animation in our desktop
anim.save('growingCoil.mp4', writer = 'ffmpeg', fps = 30)


Python3
from matplotlib import pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
 
# initializing a figure in
# which the graph will be plotted
fig = plt.figure()
 
# marking the x-axis and y-axis
axis = plt.axes(xlim =(0, 4),
                ylim =(-2, 2))
 
# initializing a line variable
line, = axis.plot([], [], lw = 3)
 
# data which the line will
# contain (x, y)
def init():
    line.set_data([], [])
    return line,
 
def animate(i):
    x = np.linspace(0, 4, 1000)
 
    # plots a sine graph
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
     
    return line,
 
anim = FuncAnimation(fig, animate,
                    init_func = init,
                    frames = 200,
                    interval = 20,
                    blit = True)
 
anim.save('continuousSineWave.mp4',
          writer = 'ffmpeg', fps = 30)


输出:

示例 2:

Python3

from matplotlib import pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
 
# initializing a figure in
# which the graph will be plotted
fig = plt.figure()
 
# marking the x-axis and y-axis
axis = plt.axes(xlim =(0, 4),
                ylim =(-2, 2))
 
# initializing a line variable
line, = axis.plot([], [], lw = 3)
 
# data which the line will
# contain (x, y)
def init():
    line.set_data([], [])
    return line,
 
def animate(i):
    x = np.linspace(0, 4, 1000)
 
    # plots a sine graph
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
     
    return line,
 
anim = FuncAnimation(fig, animate,
                    init_func = init,
                    frames = 200,
                    interval = 20,
                    blit = True)
 
anim.save('continuousSineWave.mp4',
          writer = 'ffmpeg', fps = 30)

输出: