【问题标题】:Make size of arrows all the same in matplotlib quiver plot在 matplotlib 箭袋图中使箭头的大小相同
【发布时间】:2021-07-15 18:06:19
【问题描述】:

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

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")

这里的主要代码:

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')

输出视频图的屏幕截图如下所示:

@Quiver matplotlib : arrow with the same sizesDerek Eden,使用以下代码使随机uv 的箭头大小相同。我怎样才能使它适应非随机存储的数据?

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)

【问题讨论】:

    标签: python-3.x matplotlib


    【解决方案1】:

    我通过使用U1V1 和标准化因子F 标准化数据解决了这个问题:

    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)
    

    具有相同大小矢量箭头的绘图结果图像:

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 2017-02-22
      • 2020-07-10
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 2022-01-18
      • 2020-07-23
      • 1970-01-01
      相关资源
      最近更新 更多