Issue
I have the following code, which is supposed to draw arrows in different directions sharing the same origin at (0,0):
import matplotlib.pyplot as plt
import numpy as np
angles = np.linspace(0, 2*np.pi, 8, endpoint=False)
X = np.zeros_like(angles)
Y = np.zeros_like(angles)
U = np.cos(angles)
V = np.sin(angles)
fig, ax = plt.subplots()
p = ax.quiver(X, Y, U, V, angles='xy', headlength=2)
plt.axis('equal')
plt.show()
However it produces a
RuntimeWarning: invalid value encountered in divide lengths = np.hypot(*dxy.T) / eps
in matplotlib\quiver.py:603 and only one arrow instead of 8 is drawn: !
What I expect is the following:
Which can be achieved by setting X or Y or both to something not equal to zero. Is this expected behaviour and how can I draw a quiver plot with all arrows originating at (0,0)?
Solution
The warning is being triggered by this line, so eps
is zero. eps
gets set here based on the data limits of your plot. So if you have other things to add to this plot, you could try adding those first so they set non-zero data limits.
Alternatively, note that this loop is entered by having set angles='xy'
. Since you set your aspect ratio to "equal", angles in screen space are the same as angles in data space. So you do not gain anything by setting angles='xy'
and can just take it out:
import matplotlib.pyplot as plt
import numpy as np
angles = np.linspace(0, 2*np.pi, 8, endpoint=False)
X = np.zeros_like(angles)
Y = np.zeros_like(angles)
U = np.cos(angles)
V = np.sin(angles)
fig, ax = plt.subplots()
p = ax.quiver(X, Y, U, V, headlength=2)
plt.axis('equal')
plt.show()
Or, if you specify your angles in degrees, you can pass those directly to quiver and U and V are only used to determine the length of the arrows. This gives the same plot as above.
import matplotlib.pyplot as plt
import numpy as np
angles = np.linspace(0, 360, 8, endpoint=False)
X = np.zeros_like(angles)
Y = np.zeros_like(angles)
U = np.ones_like(angles)
V = np.ones_like(angles)
fig, ax = plt.subplots()
p = ax.quiver(X, Y, U, V, angles=angles, headlength=2)
plt.axis('equal')
plt.show()
Answered By - RuthC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.