首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在matplotlib箭图中使箭头大小完全相同

在matplotlib箭图中使箭头大小完全相同
EN

Stack Overflow用户
提问于 2021-07-16 02:06:19
回答 1查看 45关注 0票数 0

我想让箭头大小在2D quiver plot中完全相同。在数据文件中,它们具有不同的大小:

代码语言:javascript
运行
复制
U = np.loadtxt("/home/brendan/software/tf2-model-g/arrays/quiver_array33/u.txt")
V = np.loadtxt("/home/brendan/software/tf2-model-g/arrays/quiver_array33/v.txt")

主要代码如下:

代码语言:javascript
运行
复制
nx, ny = 240, 426

x1 = range(nx)
y1 = range(ny)

#data
U = np.loadtxt("/home/brendan/software/tf2-model-g/arrays/quiver_array33/u.txt")
V = np.loadtxt("/home/brendan/software/tf2-model-g/arrays/quiver_array33/v.txt")
            
X1, Y1 = np.meshgrid(x1, y1)  # `plot_surface` expects `x` and `y` data to be 2D
            
fig, hf = plt.subplots(figsize=(10,10))
hf.set_title("Model G fluid velocity vector field - every 5th arrow, time = " + str(c1))
Q = hf.quiver(X1[::5, ::5], Y1[::5, ::5], U[::5, ::5], V[::5, ::5], units='width',
      pivot='mid', scale=0.1, headwidth=7)#pivot='mid', units='inches')
qk = hf.quiverkey(Q, 0.9, 0.9, 0.1, r'$\frac{distance}{time}$', labelpos='E',
      coordinates='figure')
hf.scatter(X1[::5, ::5], Y1[::5, ::5], color='r', s=5)

plt.savefig('/home/brendan/software/tf2-model-g/plots/2D_video40/2D_video_velocity_' + str(c1) + '.png')

输出video-plot的截图如下所示:

@ Quiver matplotlib : arrow with the same sizes Derek Eden使用以下代码使随机uv的箭头大小完全相同。我如何使其适用于非随机存储的数据?

代码语言:javascript
运行
复制
x = np.arange(10)
y = np.arange(10)
xx, yy = np.meshgrid(x,y)
u = np.random.uniform(low=-1, high=1, size=(9,9))
v = np.random.uniform(low=-1, high=1, size=(9,9))
plt.quiver(xx, yy, u, v)
EN

回答 1

Stack Overflow用户

发布于 2021-07-16 13:36:29

我通过使用U1V1和归一化因子F对数据进行归一化处理,解决了这个问题

代码语言:javascript
运行
复制
U = np.loadtxt("/home/brendan/software/tf2-model-g/arrays/quiver_array33/u.txt")
V = np.loadtxt("/home/brendan/software/tf2-model-g/arrays/quiver_array33/v.txt")

N = np.sqrt( U**2 + V**2 )  # normalize data
U1, V1 = U/N, V/N

#F = 0.001 # normalisation factor
F = 0.004

U1 *= F
V1 *= F

X1, Y1 = np.meshgrid(x1, y1)  # `plot_surface` expects `x` and `y` data to be 2D

fig, hf = plt.subplots(figsize=(10,10))
hf.set_title("Model G fluid velocity vector field - every 5th arrow, time = " + str(c1))
Q = hf.quiver(X1[::5, ::5], Y1[::5, ::5], U1[::5, ::5], V1[::5, ::5], units='width',
     pivot='mid', scale=0.1, headwidth=5)
qk = hf.quiverkey(Q, 0.9, 0.9, 0.1, r'$\frac{distance}{time}$', labelpos='E',
     coordinates='figure')
hf.scatter(X1[::5, ::5], Y1[::5, ::5], color='c', s=0)

使用相同大小的矢量箭头生成的绘图图像:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68398823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档