如何使用 Matplotlib 绘制二维数学向量?

matplotlibserver side programmingprogramming更新于 2025/7/4 11:52:17

要使用 Matplotlib 绘制二维数学向量,我们可以采取以下步骤−

  • 使用 NumPy 数组创建向量坐标。
  • 获取 x、y、u 和 v 数据点。
  • 使用 figure 方法创建新图形或激活现有图形。
  • 使用 gca() 方法获取当前轴。
  • 设置轴的 x 和 y 坐标。
  • 要重新绘制当前图形,请使用 draw() 方法。
  • 要显示图形,请使用 show() 方法。

示例

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
soa = np.array([[0, 0, 3, 2], [0, 0, 4, 5], [0, 0, 9, 9]])
X, Y, U, V = zip(*soa)
plt.figure()
ax = plt.gca()
ax.quiver(X, Y, U, V, angles='xy', scale_units='xy', scale=1, color=['red', 'green', 'yellow'])
ax.set_xlim([-1, 10])
ax.set_ylim([-1, 10])
plt.draw()
plt.show()

输出


相关文章