The Python Oracle

Quiver matplotlib : arrow with the same sizes

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor

--

Chapters
00:00 Question
00:33 Accepted answer (Score 6)
01:42 Thank you

--

Full question
https://stackoverflow.com/questions/6092...

Accepted answer links:
[image]: https://i.stack.imgur.com/SqolP.png
[image]: https://i.stack.imgur.com/dm5Md.png

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #matplotlib #normalize

#avk47



ACCEPTED ANSWER

Score 7


Not sure if there is a way to do this by explicitly providing a kwarg to plt.quiver, but a quick work around would be like this:

original plot

x = np.arange(10)
y = np.arange(10)
xx, yy = np.meshgrid(x,y)
u = np.random.uniform(low=-1, high=1, size=(10,10))
v = np.random.uniform(low=-1, high=1, size=(10,10))
plt.quiver(xx, yy, u, v)

original

normalized plot

r = np.power(np.add(np.power(u,2), np.power(v,2)),0.5) #could do (u**2+v**2)**0.5, other ways...
plt.quiver(xx, yy, u/r, v/r)

enter image description here

this just normalizes the u and v components of the vector by the magnitude of the vector, thereby maintaining the proper direction, but scaling down the magnitude of each arrow to 1