Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 117f4ac

Browse files
tacaswellMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #13689: BUG: fix scaling of quiverkey when quiver scale_units='xy'
1 parent 6bcd43e commit 117f4ac

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/matplotlib/quiver.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,11 @@ def _init(self):
304304
# Hack: save and restore the Umask
305305
_mask = self.Q.Umask
306306
self.Q.Umask = ma.nomask
307+
u = self.U * np.cos(np.radians(self.angle))
308+
v = self.U * np.sin(np.radians(self.angle))
309+
angle = self.Q.angles if isinstance(self.Q.angles, str) else 'uv'
307310
self.verts = self.Q._make_verts(
308-
np.array([self.U]), np.zeros(1), self.angle)
311+
np.array([u]), np.array([v]), angle)
309312
self.Q.Umask = _mask
310313
self.Q.pivot = _pivot
311314
kw = self.Q.polykw

lib/matplotlib/tests/test_quiver.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,31 @@ def test_quiver_key_pivot():
129129
ax.quiverkey(q, 0, 0.5, 1, 'W', labelpos='W')
130130

131131

132+
@image_comparison(baseline_images=['quiver_key_xy'],
133+
extensions=['png'], remove_text=True)
134+
def test_quiver_key_xy():
135+
# With scale_units='xy', ensure quiverkey still matches its quiver.
136+
# Note that the quiver and quiverkey lengths depend on the axes aspect
137+
# ratio, and that with angles='xy' their angles also depend on the axes
138+
# aspect ratio.
139+
X = np.arange(8)
140+
Y = np.zeros(8)
141+
angles = X * (np.pi / 4)
142+
uv = np.exp(1j * angles)
143+
U = uv.real
144+
V = uv.imag
145+
fig, axs = plt.subplots(2)
146+
for ax, angle_str in zip(axs, ('uv', 'xy')):
147+
ax.set_xlim(-1, 8)
148+
ax.set_ylim(-0.2, 0.2)
149+
q = ax.quiver(X, Y, U, V, pivot='middle',
150+
units='xy', width=0.05,
151+
scale=2, scale_units='xy',
152+
angles=angle_str)
153+
for x, angle in zip((0.2, 0.5, 0.8), (0, 45, 90)):
154+
ax.quiverkey(q, X=x, Y=0.8, U=1, angle=angle, label='', color='b')
155+
156+
132157
@image_comparison(baseline_images=['barbs_test_image'],
133158
extensions=['png'], remove_text=True)
134159
def test_barbs():

0 commit comments

Comments
 (0)