使用 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.