python pyplot.quiver:叠加向量场的缩放问题

0 投票
1 回答
2554 浏览
提问于 2025-04-18 09:02

我想在一个箭头图中绘制两个向量场。现在已经可以做到:在每个点上,有两个不同颜色的向量。不过现在的问题是,它们的缩放比例不一样:比如说,第一个向量场中的向量 (1,0) 和第二个向量场中的 (1,0) 显示的长度不同。

我该怎么做才能让这个箭头图中的两个向量场使用相同的缩放比例,这样在我的图中,res 的 (1,0) 和 sol 的 (1,0) 就能有相同的实际长度呢?

我的代码:

import numpy as np
from matplotlib import pyplot as plt

#res and sol are two vector fields with dimension (ny, nx ,2)

step=3 #not all vectors should be plotted. Just every third one (in x and y direction)

X,Y = np.meshgrid(np.arange(0,nx,step), np.arange(0,ny,step))            
Q=plt.quiver(X,Y,sol[::step,::step,0], sol[::step,::step,1], color='r')    
W=plt.quiver(X,Y,res[::step,::step,0], res[::step,::step,1], color='b')
plt.quiverkey(Q, 0.4, 0.15, 1, r'text1', labelpos='S')
plt.quiverkey(W, 0.6, 0.15, 1, r'text2', labelpos='S')
plt.show()

1 个回答

1

你有两个解决方案:

  1. 对输入的数据进行标准化和缩放
  2. 告诉quiver如何缩放你的数据

第二个方案(最快):

Q=plt.quiver(X,Y,sol[::step,::step,0], sol[::step,::step,1], color='r', scale=1)
W=plt.quiver(X,Y,res[::step,::step,0], res[::step,::step,1], color='b', scale=1)

你可以调整缩放值,但你必须在两个quiver中保持相同的值,这样才能得到你想要的结果。这个讨论串提供了一些帮助,但在你的情况下,我认为给每个quiver相同的缩放值并不是过度要求。

撰写回答