简体   繁体   中英

How do you draw a sector in matplotlib?

I would like just a pure sector line, not a wedge. I am trying to use it to represent the uncertainty of a vector's direction, at the tip of the vector arrow. I am using plt.arrow to plot the arrow over a basemap in matplotlib.

I haven't been able to find any way to express this uncertainty other than with a wedge. I was wondering if it is possible to plot just a sector line at the tip of the arrow.

Assuming you have an arbitrary vector v relative to some origin v_origin , and it's angular uncertainty in degrees:

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM