Him
Him

Reputation: 5551

How to turn off matplotlib quiver scaling?

The matplotlib.pyplot.quiver function takes a set of "origin" points and a set of "destination" points and the plots a bunch of arrows starting at the "origin" points headed in the direction of the "destination" points. However, there is a scaling factor so that the arrows don't necessarily end AT the "destination" points, they simply point in that direction.

e.g.

import matplotlib.pyplot as plt
import numpy as np

pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])

plt.quiver(pts[:,0], pts[:,1], end_pts[:,0], end_pts[:,1])

the_picture

Note that the vector in the bottom left starts at (1,2) (which I want), but does not end at (2,4). This is governed by a scale parameter to the quiver function that makes the arrow longer or shorter. How do I get the arrow to end at EXACTLY (2,4)?

Upvotes: 4

Views: 2447

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

The quiver documentation states

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.

Note however that u and v are understood relative to the position. Hence you would need to take the difference first.

import matplotlib.pyplot as plt
import numpy as np

pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])
diff = end_pts - pts

plt.quiver(pts[:,0], pts[:,1], diff[:,0], diff[:,1],
           angles='xy', scale_units='xy', scale=1.)

plt.show()

enter image description here

Upvotes: 7

Related Questions