演示高级箭图和箭袋功能

箭袋)展示一些更高级的选项。有关简单示例,请参阅 Quiver Simple Demo

已知问题:自动缩放图未考虑箭头,因此边界上的那些通常不在图中。以完全一般的方式解决这个问题并不容易。解决方法是手动展开Axes对象。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
  4. U = np.cos(X)
  5. V = np.sin(Y)
  6. fig1, ax1 = plt.subplots()
  7. ax1.set_title('Arrows scale with plot width, not view')
  8. Q = ax1.quiver(X, Y, U, V, units='width')
  9. qk = ax1.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E',
  10. coordinates='figure')

箭图示例

  1. fig2, ax2 = plt.subplots()
  2. ax2.set_title("pivot='mid'; every third arrow; units='inches'")
  3. Q = ax2.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3],
  4. pivot='mid', units='inches')
  5. qk = ax2.quiverkey(Q, 0.9, 0.9, 1, r'$1 \frac{m}{s}$', labelpos='E',
  6. coordinates='figure')
  7. ax2.scatter(X[::3, ::3], Y[::3, ::3], color='r', s=5)

箭图示例2

  1. fig3, ax3 = plt.subplots()
  2. ax3.set_title("pivot='tip'; scales with x view")
  3. M = np.hypot(U, V)
  4. Q = ax3.quiver(X, Y, U, V, M, units='x', pivot='tip', width=0.022,
  5. scale=1 / 0.15)
  6. qk = ax3.quiverkey(Q, 0.9, 0.9, 1, r'$1 \frac{m}{s}$', labelpos='E',
  7. coordinates='figure')
  8. ax3.scatter(X, Y, color='k', s=5)
  9. plt.show()

箭图示例3

参考

此示例中显示了以下函数和方法的用法:

  1. import matplotlib
  2. matplotlib.axes.Axes.quiver
  3. matplotlib.pyplot.quiver
  4. matplotlib.axes.Axes.quiverkey
  5. matplotlib.pyplot.quiverkey

下载这个示例