开发者

Matplotlib quiver scale

I'm trying to plot some arrows using matploblib with the quiver function. But I want to choose the length of each arrow individually using an array.

http://matplotlib.sourceforge.net开发者_如何学Python/api/pyplot_api.html#matplotlib.pyplot.quiver http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html

In these demos and documentation, it is show that you can change scales proportionally to units (x,y, width, height, xy, inches, ...), is there a way to define a scale for each arrow?


To specify each arrow's location and vector and length is an over-specification of the quiver plot. So what you need to do is change the data that you are plotting.

If you have the vector field U and V (same U and V as your examples), you can normalize them by:

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

Then you can apply whatever scaling factor array you want:

U2 *= F
V2 *= F


If you would like to have some notion of relative vector magnitudes for two vectors, say x and y, you could try this nonlinear scaling:

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

You can change the range of vector magnitudes by increasing or decreasing the value of alpha.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜