使用 Matplotlib 在 Python 中繪製動畫箭袋


要在 Python 中為箭袋製作動畫,我們可以採取以下步驟 −

  • 設定圖形大小並調整子圖之間和周圍的邊距。
  • 使用 numpy 建立xy資料點。
  • 使用 numpy 建立uv資料點。
  • 建立圖形和一組子圖。
  • 使用quiver()方法繪製二維箭頭場。
  • 要使箭袋動畫化,我們可以在animate()方法中更改uv的值。更新uv的值以及向量的顏色。
  • 要顯示圖形,請使用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()

輸出

更新日期: 09-Jun-2021

2K+ 瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.