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

Skip to content

Commit e0b151a

Browse files
committed
Cleanup QuiverKey init and deprecate some attributes.
Ensure that QuiverKey.vector exists as soon as the constructor exits, rather than waiting for the first draw. Deprecate the `fontproperties`, `labelcolor`, and `verts` attribute (overwriting them would not actually update the underlying artist (except before the first draw) anyways). Also deprecate `kw` (which *could* be updated with effect, but it seems simpler to directly update the underlying artist here too, if really needed; moreover the old version, which mutated `self.Q.polykw` *in-place*, likely led to weird side-effects e.g. if a key is first added to `kw`, a draw is triggered, mutating `self.Q.polykw`, then the key is removed).
1 parent c4b9963 commit e0b151a

4 files changed

Lines changed: 56 additions & 35 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``kw``, ``fontproperties``, ``labelcolor``, and ``verts`` attributes of ``QuiverKey``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
These attributes are deprecated (note that ``fontproperties``, ``labelcolor``,
4+
or ``verts`` after the first draw had no effect previously). Directly
5+
access the relevant attributes on the sub-artists ``QuiverKey.vector`` and
6+
``QuiverKey.text``, instead.

lib/matplotlib/quiver.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -345,47 +345,57 @@ def __init__(self, Q, X, Y, U, label,
345345
self._labelsep_inches = labelsep
346346

347347
self.labelpos = labelpos
348-
self.labelcolor = labelcolor
349-
self.fontproperties = fontproperties or dict()
350-
self.kw = kwargs
348+
self._kw = kwargs # Remove when kw deprecation elapses.
349+
self.vector = mcollections.PolyCollection(
350+
[], **{**self.Q.polykw, **kwargs})
351351
self.text = mtext.Text(
352352
text=label,
353353
horizontalalignment=self.halign[self.labelpos],
354354
verticalalignment=self.valign[self.labelpos],
355-
fontproperties=self.fontproperties)
356-
if self.labelcolor is not None:
357-
self.text.set_color(self.labelcolor)
355+
fontproperties=fontproperties or {})
356+
if labelcolor is not None:
357+
self.text.set_color(labelcolor)
358358
self._dpi_at_last_init = None
359359
self.zorder = zorder if zorder is not None else Q.zorder + 0.1
360360

361+
kw = _api.deprecated("3.11")( # Also remove self._kw when deprecation elapses.
362+
property(lambda self: self._kw))
363+
fontproperties = _api.deprecated(
364+
"3.11", alternative="quiverkey.text.get_fontproperties()")(
365+
property(lambda self: self.text.get_fontproperties()))
366+
labelcolor = _api.deprecated(
367+
"3.11", alternative="quiverkey.text.get_color()")(
368+
property(lambda self: self.text.get_color()))
369+
verts = _api.deprecated(
370+
"3.11", alternative="[p.vertices for p in quiverkey.vector.get_paths()]")(
371+
property(lambda self: [p.vertices for p in self.vector.get_paths()]))
372+
361373
@property
362374
def labelsep(self):
363375
return self._labelsep_inches * self.Q.axes.get_figure(root=True).dpi
364376

365377
def _init(self):
366-
if True: # self._dpi_at_last_init != self.axes.get_figure().dpi
367-
if self.Q._dpi_at_last_init != self.Q.axes.get_figure(root=True).dpi:
368-
self.Q._init()
369-
self._set_transform()
370-
with cbook._setattr_cm(self.Q, pivot=self.pivot[self.labelpos],
371-
# Hack: save and restore the Umask
372-
Umask=ma.nomask):
373-
u = self.U * np.cos(np.radians(self.angle))
374-
v = self.U * np.sin(np.radians(self.angle))
375-
self.verts = self.Q._make_verts([[0., 0.]],
376-
np.array([u]), np.array([v]), 'uv')
377-
kwargs = self.Q.polykw
378-
kwargs.update(self.kw)
379-
self.vector = mcollections.PolyCollection(
380-
self.verts,
381-
offsets=[(self.X, self.Y)],
382-
offset_transform=self.get_transform(),
383-
**kwargs)
384-
if self.color is not None:
385-
self.vector.set_color(self.color)
386-
self.vector.set_transform(self.Q.get_transform())
387-
self.vector.set_figure(self.get_figure())
388-
self._dpi_at_last_init = self.Q.axes.get_figure(root=True).dpi
378+
if False: # self._dpi_at_last_init == self.axes.get_figure().dpi
379+
return
380+
if self.Q._dpi_at_last_init != self.Q.axes.get_figure(root=True).dpi:
381+
self.Q._init()
382+
self._set_transform()
383+
with cbook._setattr_cm(self.Q, pivot=self.pivot[self.labelpos],
384+
# Hack: save and restore the Umask
385+
Umask=ma.nomask):
386+
u = self.U * np.cos(np.radians([self.angle]))
387+
v = self.U * np.sin(np.radians([self.angle]))
388+
verts = self.Q._make_verts([[0., 0.]], u, v, 'uv')
389+
self.vector.set(
390+
verts=verts,
391+
offsets=[(self.X, self.Y)],
392+
offset_transform=self.get_transform(),
393+
transform=self.Q.get_transform(),
394+
figure=self.get_figure(),
395+
)
396+
if self.color is not None:
397+
self.vector.set_color(self.color)
398+
self._dpi_at_last_init = self.Q.axes.get_figure(root=True).dpi
389399

390400
def _text_shift(self):
391401
return {

lib/matplotlib/quiver.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ class QuiverKey(martist.Artist):
2525
color: ColorType | None
2626
label: str
2727
labelpos: Literal["N", "S", "E", "W"]
28-
labelcolor: ColorType | None
29-
fontproperties: dict[str, Any]
30-
kw: dict[str, Any]
3128
text: Text
3229
zorder: float
3330
def __init__(
@@ -49,6 +46,14 @@ class QuiverKey(martist.Artist):
4946
**kwargs
5047
) -> None: ...
5148
@property
49+
def kw(self) -> dict[str, Any]: ...
50+
@property
51+
def fontproperties(self) -> dict[str, Any]: ...
52+
@property
53+
def labelcolor(self) -> ColorType | None: ...
54+
@property
55+
def verts(self) -> Sequence[ArrayLike]: ...
56+
@property
5257
def labelsep(self) -> float: ...
5358
def set_figure(self, fig: Figure | SubFigure) -> None: ...
5459

lib/matplotlib/tests/test_quiver.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def test_quiverkey_angles():
266266
qk = ax.quiverkey(q, 1, 1, 2, 'Label')
267267
# The arrows are only created when the key is drawn
268268
fig.canvas.draw()
269-
assert len(qk.verts) == 1
269+
assert len(qk.vector.get_paths()) == 1
270270

271271

272272
def test_quiverkey_angles_xy_aitoff():
@@ -295,7 +295,7 @@ def test_quiverkey_angles_xy_aitoff():
295295
qk = ax.quiverkey(q, 0, 0, 1, '1 units')
296296

297297
fig.canvas.draw()
298-
assert len(qk.verts) == 1
298+
assert len(qk.vector.get_paths()) == 1
299299

300300

301301
def test_quiverkey_angles_scale_units_cartesian():
@@ -322,7 +322,7 @@ def test_quiverkey_angles_scale_units_cartesian():
322322
qk = ax.quiverkey(q, 0, 0, 1, '1 units')
323323

324324
fig.canvas.draw()
325-
assert len(qk.verts) == 1
325+
assert len(qk.vector.get_paths()) == 1
326326

327327

328328
def test_quiver_setuvc_numbers():

0 commit comments

Comments
 (0)