使用 Matplotlib 與子圖和 ArtistAnimation 的動畫
如需使用 Matplotlib 和子圖與 ArtistAnimation 建立動畫,我們可以執行以下步驟:
- 設定圖形尺寸,調整子圖之間和周圍的填充。
- 建立圖形和一組子圖。
- 建立一個使用者自定義函式 Init 以繪製一個清晰的幀。
- 使用 FuncAnimation 透過反覆呼叫函式 *func* 建立動畫。
- 定義一個 animate 函式以更新 FuncArtist 類中的資料點。
- 要顯示圖形,請使用 show() 方法。
示例
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() xdata, ydata = [], [] ln, = plt.plot([], [], 'r*') def init(): ax.set_xlim(0, 100) ax.set_ylim(-1, 1) return ln, def animate(frame): xdata.append(frame) ydata.append(np.sin(frame)) ln.set_data(xdata, ydata) return ln, ani = FuncAnimation(fig, animate, init_func=init, blit=True, frames=100) plt.show()
輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP