【问题标题】:Matplotlib quiver scaleMatplotlib 箭袋规模
【发布时间】:2011-05-06 12:30:17
【问题描述】:

我正在尝试使用带有 quiver 函数的 matploblib 绘制一些箭头。但我想使用数组单独选择每个箭头的长度。

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.quiver http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html

在这些演示和文档中,表明您可以根据单位(x,y,宽度,高度,xy,英寸,...)按比例更改比例,有没有办法为每个箭头定义比例?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    指定每个箭头的位置和矢量长度是对箭袋图的过度指定。所以你需要做的是改变你正在绘制的数据。

    如果您有向量场 U 和 V(与您的示例相同的 U 和 V),您可以通过以下方式对它们进行归一化:

    N = numpy.sqrt(U**2+V**2)  # there may be a faster numpy "normalize" function
    U2, V2 = U/N, V/N
    

    然后你可以应用你想要的任何缩放因子数组:

    U2 *= F
    V2 *= F
    

    【讨论】:

      【解决方案2】:

      如果您想了解两个向量(例如 x 和 y)的相对向量幅度的概念,您可以尝试这种非线性缩放:

      L = np.sqrt(x**2 + y**2)
      U = x / L 
      V = y / L 
      #If we just plotted U and V all the vectors would have the same length since 
      #they've been normalized.
      
      m = np.max(L)
      alpha = .1 
      #m is the largest vector, it will correspond to a vector with 
      #magnitude alpha in the quiver plot
      
      S=alpha /(1+np.log(m/L)) 
      
      U1=S*U 
      
      V1=S*V
      
      plt.figure() 
      Q = plt.quiver(x,y,U1,V1,scale = 1., units='width') 
      
      qk = plt.quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', 
      labelpos ='E',coordinates='figure')
      

      您可以通过增加或减少 alpha 的值来更改矢量幅度的范围。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 2016-07-04
        相关资源
        最近更新 更多