使用 Matplotlib 在 Python 中繪製動畫箭袋
要在 Python 中為箭袋製作動畫,我們可以採取以下步驟 −
- 設定圖形大小並調整子圖之間和周圍的邊距。
- 使用 numpy 建立x 和y資料點。
- 使用 numpy 建立u 和v資料點。
- 建立圖形和一組子圖。
- 使用quiver()方法繪製二維箭頭場。
- 要使箭袋動畫化,我們可以在animate()方法中更改u 和v的值。更新u 和v的值以及向量的顏色。
- 要顯示圖形,請使用show()方法。
示例
import numpy as np import random as rd from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x, y = np.mgrid[:2 * np.pi:10j, :2 * np.pi:5j] u = np.cos(x) v = np.sin(y) fig, ax = plt.subplots(1, 1) qr = ax.quiver(x, y, u, v, color='red') def animate(num, qr, x, y): u = np.cos(x + num * 0.1) v = np.sin(y + num * 0.1) qr.set_UVC(u, v) qr.set_color((rd.random(), rd.random(), rd.random(), rd.random())) return qr, anim = animation.FuncAnimation(fig, animate, fargs=(qr, x, y), interval=50, blit=False) plt.show()
輸出

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