Matplotlib是Python中令人驚歎的可視化庫,用於數組的二維圖。 Matplotlib是一個基於NumPy數組的多平台數據可視化庫,旨在與更廣泛的SciPy堆棧配合使用。
matplotlib.animation.FuncAnimation
matplotlib.animation.FuncAnimation類用於通過重複調用同一函數(例如func)來製作動畫。
用法: class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, *, cache_frame_data=True, **kwargs)
Paramcontribute.geeksforgeeks.org/wp-admin/post.php?post=1675187&action;=編輯者:
- fig:它是用於繪製,調整大小或其他任何所需事件的圖形對象。
可以通過fargs參數提供任何其他位置參數。
- func:每次都會調用可調用函數。通過第一個參數給出幀中的下一個值。其他任何其他位置參數是通過fargs參數給出的。如果blit值等於True,則func返回所有要修改或創建的藝術家的迭代。劃線算法使用此數據來確定必須更新圖形的哪些部分。如果blit == False,則返回的值未使用或省略。
- frames:它可以是可迭代的,整數,生成器函數或無。它是一個可選參數。它是要傳遞給func和動畫每一幀的數據源。
- init_func:它是一個可選的可調用函數,用於繪製清晰的框架。
- fargs:它是一個可選參數,可以是元組或None,這是需要傳遞給func的每個調用的附加參數。
- save_count:它是一個整數,用作從幀到緩存的值數量的後備。僅在無法從幀推斷幀數時使用,即當它是沒有長度的迭代器或生成器時。默認值為100。
- interval:它是一個可選的整數值,表示每幀之間的延遲(以毫秒為單位)。默認值為100。
- repeat_delay:它是一個可選的整數值,它在重複動畫之前添加以毫秒為單位的延遲。默認為無。
- blit:它是一個可選的布爾參數,用於控製繪圖以優化圖形。
- cache_frame_data:它是一個可選的布爾參數,用於控製數據的緩存。默認為True。
該類的方法:
方法 | 描述 |
---|---|
__init__(self, fig, func[, frames, …]) | 初始化自我。 |
new_frame_seq(self) | 返回新的幀信息序列。 |
new_saved_frame_seq(self) | 返回新的保存/緩存幀信息序列。 |
save(self, filename[, writer, fps, dpi, …]) | 通過繪製每幀將動畫另存為電影文件。 |
to_html5_video(self[, embed_limit]) | 將動畫轉換為HTML5 <video>標簽。 |
to_jshtml(self[, fps, embed_frames, …]) | 生成動畫的HTML表示 |
範例1:
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 coordinates
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)
輸出:
範例2:
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)
輸出:
相關用法
- Python Matplotlib.ticker.MultipleLocator用法及代碼示例
- Python Matplotlib.gridspec.GridSpec用法及代碼示例
- Python Matplotlib.patches.CirclePolygon用法及代碼示例
- Python Matplotlib.colors.Normalize用法及代碼示例
注:本文由純淨天空篩選整理自RajuKumar19大神的英文原創作品 Matplotlib.animation.FuncAnimation class in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。