matplotlib quiver 清奇的腦回路


matplotlib quiver:清奇的腦回路

    Call signature::
    
      quiver([X, Y], U, V, [C], **kw)
      
angles : {'uv', 'xy'} or array-like, optional, default: 'uv'
        Method for determining the angle of the arrows.
    
        - 'uv': The arrow axis aspect ratio is 1 so that
          if *U* == *V* the orientation of the arrow on the plot is 45 degrees
          counter-clockwise from the horizontal axis (positive to the right).
    
          Use this if the arrows symbolize a quantity that is not based on
          *X*, *Y* data coordinates.
    
        - 'xy': Arrows point from (x, y) to (x+u, y+v).
          Use this for plotting a gradient field, for example.
    
        - Alternatively, arbitrary angles may be specified explicitly as an array
          of values in degrees, counter-clockwise from the horizontal axis.
    
          In this case *U*, *V* is only used to determine the length of the
          arrows.
    
        Note: inverting a data axis will correspondingly invert the
        arrows only with ``angles='xy'``.
    
    scale : float, optional
        Number of data units per arrow length unit, e.g., m/s per plot width; a
        smaller scale parameter makes the arrow longer. Default is *None*.
    
        If *None*, a simple autoscaling algorithm is used, based on the average
        vector length and the number of vectors. The arrow length unit is given by
        the *scale_units* parameter.
    
    scale_units : {'width', 'height', 'dots', 'inches', 'x', 'y', 'xy'}, optional
        If the *scale* kwarg is *None*, the arrow length unit. Default is *None*.
    
        e.g. *scale_units* is 'inches', *scale* is 2.0, and ``(u, v) = (1, 0)``,
        then the vector will be 0.5 inches long.
    
        If *scale_units* is 'width' or 'height', then the vector will be half the
        width/height of the axes.
    
        If *scale_units* is 'x' then the vector will be 0.5 x-axis
        units. To plot vectors in the x-y plane, with u and v having
        the same units as x and y, use
        ``angles='xy', scale_units='xy', scale=1``.

angles

angles這個參數的目的非常簡單,因為圖的寬和高可能不同,所以x方向的單位長度和y方向的單位長度可能不同,這時我們需要做出選擇,一是不管長度對不對,角度一定要對,此時angles='uv',二是不管角度了,只要長度對就可以了,此時angles='xy'

plt.quiver(0, 0, 1, 1, angles='uv', scale_units='x', scale=1)
plt.xticks(np.arange(0, 6))
plt.yticks(np.arange(0, 6))
plt.grid()
plt.savefig('fig01.png')

plt.quiver(0, 0, 1, 1, angles='xy', scale_units='xy', scale=1)
plt.xticks(np.arange(0, 6))
plt.yticks(np.arange(0, 6))
plt.grid()
plt.savefig('fig02.png')

如上,angle='uv'保證了角度是45度,因為是(1,1)向量,而angle='xy'保證了在x,y方向長度都為1.

scale_units='x', scale=1是說對於對象(1, 1)向量的單位長度在圖中就表示為x方向的單位長度。注意這里有兩個單位長度,前一個是數據的單位長度,后一個是在圖中用多長的線段表示。

angles='xy', scale_units='xy', scale=1就是表示數學中向量的最好方法,(1, 1)向量表示在圖中也是(1, 1)向量。

plt.axis('square')
plt.quiver(0, 0, 1, 1, angles='uv', scale_units='x', scale=1)
plt.xticks(np.arange(0, 6))
plt.yticks(np.arange(0, 6))
plt.grid()
plt.savefig('fig03.png')

plt.axis('square')
plt.quiver(0, 0, 1, 1, angles='xy', scale_units='xy', scale=1)
plt.xticks(np.arange(0, 6))
plt.yticks(np.arange(0, 6))
plt.grid()
plt.savefig('fig04.png')

這兩個圖說明如果圖中x, y方向的單位長度相等,則angles='uv'和angles='xy'沒有區別。

scale_units和scale

plt.axis('square')
plt.quiver(0, 0, 0, 1, scale_units='height', scale=1)
plt.xticks(np.arange(0, 6))
plt.yticks(np.arange(0, 6))
plt.grid()
plt.savefig('fig05.png')

plt.axis('square')
plt.quiver(0, 0, 0, 1, scale_units='height', scale=5)
plt.xticks(np.arange(0, 6))
plt.yticks(np.arange(0, 6))
plt.grid()
plt.savefig('fig06.png')

(0, 1)矢量的長度為1(單位長度),在圖中用1/scale * scale_units的長度表示。

scale_units='height', scale=1,表示(0, 1)矢量用一整個子圖(axes)的高度來表示。

scale_unitsj='height', scale=5, 表示(0, 1)矢量用1/5子圖(axes)的高度來表示。

所以可能matplotlib quiver是可以配合quiverkey來絕對的反應速度的大小,但可能quiver更適合來反應樣本點之間速度的相對大小,如果一定要反應速度的絕對大小,可以疊加速度的contourf(要注意疊加次序,應當是contourf上疊加quiver)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM