微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

颤动矢量箭头未与 pcolormesh 中的原始网格点对齐

如何解决颤动矢量箭头未与 pcolormesh 中的原始网格点对齐

我正在绘制沙丘上的流速图,我在 python 中使用 matplotlib 使用以下代码执行此操作:

import matplotlib.pyplot as plt
import numpy as np
x = [0.0,0.03,0.06,0.09,0.29,0.49]
z = [0.0,0.18,0.27]
V = [-0.4,-0.2,-0.5,-0.6,-0.1,0.2,0.1,0.05,-0.05,0.3,0.5,0.4,0.7,0.6,0.8,0.9]
V = np.array(V)
V= V.reshape(4,6)
fig,ax = plt.subplots()
veLocity = ax.pcolormesh(x,z,V,shading='gouraud',cmap='coolwarm',vmin=-0.8,vmax=0.8)
fig.suptitle('streamwise veLocity over dunes')
fig.colorbar(veLocity,label='m/s')
plt.xlabel('streamwise distance (m)')
plt.ylabel('height (m)')
xfill = [0.0,0.49]
yfill = [0.07,0.07]
plt.fill_between(xfill,yfill,color='white')
plt.plot(xfill,color='white')
plt.show()

这给了我以下输出

pcolormesh plot flow velocities over dune

然而,我想将向量添加到速度观察中,我尝试使用 plt.quiver:

import matplotlib.pyplot as plt
import numpy as np
x = [0.0,color='white')

x,z = np.meshgrid(x,z)
plt.quiver(x,V)
plt.show()

不幸的是,速度箭头不再与 x 和 z 轴匹配,而是遵循整数:

velocity vectors,not aligned with original coordinates and also tend upwards

此外,速度向量也倾向于指向垂直方向,而我只想让速度向量指向水平方向(向右为正,或向左因此为负)

有没有办法解决这两个问题?

解决方法

我建议查看 quiverdocumentation

您遗漏了一个论点。 添加零作为速度的 y 分量应该可以解决您的两个问题。

U = np.zeros_like(V)
plt.quiver(x,z,V,U,pivot='mid')

# Update the limits to make all the velocities visible
ax.set_xlim(x.min()-0.05,x.max()+0.05)
ax.set_ylim(z.min()-0.01,z.max()+0.01)

另外,注意 u 通常表示 x 方向的速度,v 表示 y 方向的速度;也许可以考虑重命名您的变量。

streamwise velocity over dunese - horizontal quiver

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。