【问题标题】:How do you draw a sector in matplotlib?你如何在 matplotlib 中绘制一个扇区?
【发布时间】:2016-11-22 23:46:08
【问题描述】:

我只想要一条纯扇形线,而不是楔形。我试图用它来表示矢量方向的不确定性,在矢量箭头的尖端。我正在使用 plt.arrow 在 matplotlib 的底图上绘制箭头。

我找不到任何方法来表达这种不确定性,除了用楔子。我想知道是否可以在箭头尖端绘制一条扇形线。

【问题讨论】:

    标签: python matplotlib sector


    【解决方案1】:

    假设您有一个相对于某个原点 v_origin 的任意向量 v,它是以度为单位的角度不确定性:

    import pylab as plt
    import numpy as np
    #plt.style.use('ggplot') # because it's just better ;)
    
    v_origin = [.2, .3]
    v = [1., 1.]
    angular_uncert = 20. # angular uncertainty of vector in degrees
    v_angle = np.arctan2( v[1] ,v[0] ) # starting angle
    
    # plot the arrow
    fig = plt.figure(1)
    ax = plt.gca()
    ax.arrow(v_origin[0], v_origin[1] ,v[0], v[1], 
        head_width=0.05, 
        head_length=0.1, 
        lw=2, 
        fc='#777777', 
        ec='#777777', 
        length_includes_head=True)
    
    # plot the sector line
    uncert = (angular_uncert/2.) *np.pi / 180.
    r = np.linalg.norm( v ) # length of vector
    t = np.linspace( v_angle - uncert , v_angle+uncert , 100) # angular range of sector
    x = r* np.cos(t) + v_origin[0] # sector x coords
    y = r* np.sin(t) + v_origin[1] # sector y coords
    ax.plot( x,y, lw=2, ls='--') # plot the sector
    ax.plot( v_origin[0], v_origin[1], 'o', ms=10, c='Limegreen' ) # plot the origin
    
    # adjust the figure
    ax.set_xlim(0, 1.6)
    ax.set_ylim(0, 1.6)
    ax.set_aspect('equal')
    fig.show()
    

    【讨论】:

    • 这看起来与我想要的完全一样,但我无法使用纬度和经度坐标绘制箭头。如果我有圆心的坐标和不确定性,我怎么能在地图中复制它?
    • 我添加了一个原始参数 - 也许这会有所帮助?只需确保您的矢量相对于原点的坐标
    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2014-11-20
    • 2017-11-02
    • 2011-09-07
    相关资源
    最近更新 更多