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

Skip to content

Commit 99c8151

Browse files
committed
Rename quiver argument
1 parent ec76fe3 commit 99c8151

File tree

4 files changed

+76
-19
lines changed

4 files changed

+76
-19
lines changed

doc/api/next_api_changes/behavior/20962-DS.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ the `~.units.ConversionInterface.default_units` and
88
argument to be a `~.cm.ScalarMappable` object, and
99
`~.units.ConversionInterface.convert` must be able to convert data of more than
1010
one dimension (e.g. when plotting images the data is 2D).
11+
12+
The ``Quiver`` ``units`` property is renamed ``arrow_units``
13+
------------------------------------------------------------
14+
This is to clarify the meaning of this property, and enable support for color
15+
mapped data with units in the future. The corresponding keyword argument to
16+
`~.quiver.Quiver` is also renamed.

lib/matplotlib/quiver.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
use *color* instead. The size of *C* must match the number of arrow
9090
locations.
9191
92-
units : {'width', 'height', 'dots', 'inches', 'x', 'y', 'xy'}, default: 'width'
92+
arrow_units : {'width', 'height', 'dots', 'inches', 'x', 'y', 'xy'}, default:
93+
'width'
9394
The arrow dimensions (except for *length*) are measured in multiples of
9495
this unit.
9596
@@ -99,9 +100,9 @@
99100
- 'dots', 'inches': Pixels or inches based on the figure dpi.
100101
- 'x', 'y', 'xy': *X*, *Y* or :math:`\\sqrt{X^2 + Y^2}` in data units.
101102
102-
The arrows scale differently depending on the units. For
103+
The arrows scale differently depending on the arrow units. For
103104
'x' or 'y', the arrows get larger as one zooms in; for other
104-
units, the arrow size is independent of the zoom state. For
105+
arrow units, the arrow size is independent of the zoom state. For
105106
'width or 'height', the arrow size increases with the width and
106107
height of the axes, respectively, when the window is resized;
107108
for 'dots' or 'inches', resizing does not change the arrows.
@@ -151,7 +152,7 @@
151152
``angles='xy', scale_units='xy', scale=1``.
152153
153154
width : float, optional
154-
Shaft width in arrow units; default depends on choice of units,
155+
Shaft width in arrow units; default depends on choice of arrow units,
155156
above, and number of vectors; a typical starting value is about
156157
0.005 times the width of the plot.
157158
@@ -466,11 +467,13 @@ class Quiver(mcollections.PolyCollection):
466467

467468
_PIVOT_VALS = ('tail', 'middle', 'tip')
468469

470+
@_api.rename_parameter('3.6', 'units', 'arrow_units')
469471
@docstring.Substitution(_quiver_doc)
470472
def __init__(self, ax, *args,
471473
scale=None, headwidth=3, headlength=5, headaxislength=4.5,
472-
minshaft=1, minlength=1, units='width', scale_units=None,
473-
angles='uv', width=None, color='k', pivot='tail', **kw):
474+
minshaft=1, minlength=1, arrow_units='width',
475+
scale_units=None, angles='uv', width=None, color='k',
476+
pivot='tail', **kw):
474477
"""
475478
The constructor takes one required argument, an Axes
476479
instance, followed by the args and kwargs described
@@ -504,7 +507,7 @@ def __init__(self, ax, *args,
504507
super().__init__([], offsets=self.XY, transOffset=self.transform,
505508
closed=False, **kw)
506509

507-
self.units = units
510+
self.arrow_units = arrow_units
508511
self.polykw = kw
509512
self.set_UVC(U, V, C)
510513
self._initialized = False
@@ -522,11 +525,36 @@ def on_dpi_change(fig):
522525

523526
self._cid = ax.figure.callbacks.connect('dpi_changed', on_dpi_change)
524527

528+
@property
529+
def units(self):
530+
_api.warn_deprecated(
531+
'3.6',
532+
message='%(name)s is deprecated since %(since)s. Use '
533+
'%(alternative)s instead.',
534+
name='Quiver.units',
535+
alternative='Quiver.arrow_units')
536+
return self.arrow_units
537+
538+
@units.setter
539+
def units(self, val):
540+
# Avoid emitting a warning when `ScalarMappable.__init__` sets this
541+
# to None
542+
if val is not None:
543+
_api.warn_deprecated(
544+
'3.6',
545+
message='%(name)s is deprecated since %(since)s. Use '
546+
'%(alternative)s instead.',
547+
name='Quiver.units',
548+
alternative='Quiver.arrow_units')
549+
self.arrow_units = val
550+
525551
def _convert_mappable_units(self, A):
526552
"""
527553
Since Quiver already has a .units attribute for another purpose, it's
528554
not yet possible to support units on the ScalarMappable part, so
529555
override convert units to be a no-op.
556+
557+
This method can be removed when the .units deprecation expires.
530558
"""
531559
return A
532560

@@ -602,15 +630,15 @@ def set_UVC(self, U, V, C=None):
602630
self._new_UV = True
603631
self.stale = True
604632

605-
def _dots_per_unit(self, units):
633+
def _dots_per_unit(self, arrow_units):
606634
"""
607635
Return a scale factor for converting from units to pixels
608636
"""
609-
if units in ('x', 'y', 'xy'):
610-
if units == 'x':
637+
if arrow_units in ('x', 'y', 'xy'):
638+
if arrow_units == 'x':
611639
dx0 = self.axes.viewLim.width
612640
dx1 = self.axes.bbox.width
613-
elif units == 'y':
641+
elif arrow_units == 'y':
614642
dx0 = self.axes.viewLim.height
615643
dx1 = self.axes.bbox.height
616644
else: # 'xy' is assumed
@@ -622,24 +650,24 @@ def _dots_per_unit(self, units):
622650
dx0 = np.hypot(dxx0, dyy0)
623651
dx = dx1 / dx0
624652
else:
625-
if units == 'width':
653+
if arrow_units == 'width':
626654
dx = self.axes.bbox.width
627-
elif units == 'height':
655+
elif arrow_units == 'height':
628656
dx = self.axes.bbox.height
629-
elif units == 'dots':
657+
elif arrow_units == 'dots':
630658
dx = 1.0
631-
elif units == 'inches':
659+
elif arrow_units == 'inches':
632660
dx = self.axes.figure.dpi
633661
else:
634-
raise ValueError(f'Unrecognized units: {units}')
662+
raise ValueError(f'Unrecognized arrow units: {arrow_units}')
635663
return dx
636664

637665
def _set_transform(self):
638666
"""
639667
Set the PolyCollection transform to go
640668
from arrow width units to pixels.
641669
"""
642-
dx = self._dots_per_unit(self.units)
670+
dx = self._dots_per_unit(self.arrow_units)
643671
self._trans_scale = dx # pixels per arrow width unit
644672
trans = transforms.Affine2D().scale(dx)
645673
self.set_transform(trans)

lib/matplotlib/tests/test_quiver.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from matplotlib import pyplot as plt
8+
from matplotlib._api import MatplotlibDeprecationWarning
89
from matplotlib.testing.decorators import image_comparison
910

1011

@@ -168,7 +169,7 @@ def test_quiver_key_xy():
168169
ax.set_xlim(-1, 8)
169170
ax.set_ylim(-0.2, 0.2)
170171
q = ax.quiver(X, Y, U, V, pivot='middle',
171-
units='xy', width=0.05,
172+
arrow_units='xy', width=0.05,
172173
scale=2, scale_units='xy',
173174
angles=angle_str)
174175
for x, angle in zip((0.2, 0.5, 0.8), (0, 45, 90)):
@@ -277,3 +278,25 @@ def test_quiver_setuvc_numbers():
277278

278279
q = ax.quiver(X, Y, U, V)
279280
q.set_UVC(0, 1)
281+
282+
283+
def test_units_deprecation():
284+
fig, ax = plt.subplots()
285+
286+
X, Y = np.meshgrid(np.arange(2), np.arange(2))
287+
U = V = np.ones_like(X)
288+
289+
with pytest.warns(MatplotlibDeprecationWarning,
290+
match="renamed 'arrow_units'"):
291+
q = ax.quiver(X, Y, U, V, units='xy')
292+
293+
with pytest.warns(MatplotlibDeprecationWarning,
294+
match="Quiver.units is deprecated"):
295+
assert q.units == 'xy'
296+
297+
assert q.arrow_units == 'xy'
298+
299+
with pytest.warns(MatplotlibDeprecationWarning,
300+
match="Quiver.units is deprecated"):
301+
q.units = 'x'
302+
assert q.arrow_units == 'x'

lib/matplotlib/tests/test_triangulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ def dipole_potential(x, y):
816816
linewidths=[2.0, 1.0, 1.0, 1.0])
817817
# Plots direction of the electrical vector field
818818
plt.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,
819-
units='xy', scale=10., zorder=3, color='blue',
819+
arrow_units='xy', scale=10., zorder=3, color='blue',
820820
width=0.007, headwidth=3., headlength=4.)
821821
# We are leaving ax.use_sticky_margins as True, so the
822822
# view limits are the contour data limits.

0 commit comments

Comments
 (0)