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

Skip to content

Commit cb45873

Browse files
committed
Make some QuiverKey label properties modifiable
1 parent 6799367 commit cb45873

File tree

5 files changed

+102
-21
lines changed

5 files changed

+102
-21
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
``QuiverKey`` properties are now modifiable
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The `.QuiverKey` object returned by `.pyplot.quiverkey` and
5+
`.axes.Axes.quiverkey` formerly saved various properties as attributes during
6+
initialization. However, modifying these attributes may or may not have had an
7+
effect on the final result.
8+
9+
Now all such properties have getters and setters, and may be modified after
10+
creation:
11+
12+
- `.QuiverKey.label` -> `.QuiverKey.get_label` / `.QuiverKey.set_label`
13+
- `.QuiverKey.labelcolor` -> `.QuiverKey.get_labelcolor` /
14+
`.QuiverKey.set_labelcolor`
15+
- `.QuiverKey.labelpos` -> `.QuiverKey.get_labelpos` /
16+
`.QuiverKey.set_labelpos`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
``QuiverKey`` properties are now modifiable
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The `.QuiverKey` object returned by `.pyplot.quiverkey` and
5+
`.axes.Axes.quiverkey` formerly saved various properties as attributes during
6+
initialization. However, modifying these attributes may or may not have had an
7+
effect on the final result.
8+
9+
Now all such properties have getters and setters, and may be modified after
10+
creation:
11+
12+
- `.QuiverKey.label` -> `.QuiverKey.get_label` / `.QuiverKey.set_label`
13+
- `.QuiverKey.labelcolor` -> `.QuiverKey.get_labelcolor` /
14+
`.QuiverKey.set_labelcolor`
15+
- `.QuiverKey.labelpos` -> `.QuiverKey.get_labelpos` /
16+
`.QuiverKey.set_labelpos`

lib/matplotlib/quiver.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,23 +296,66 @@ def __init__(self, Q, X, Y, U, label,
296296
self.angle = angle
297297
self.coord = coordinates
298298
self.color = color
299-
self.label = label
300299
self._labelsep_inches = labelsep
301300

302-
self.labelpos = labelpos
303-
self.labelcolor = labelcolor
301+
_api.check_in_list(['N', 'S', 'E', 'W'], labelpos=labelpos)
302+
self._labelpos = labelpos
304303
self.fontproperties = fontproperties or dict()
305304
self.kw = kwargs
306305
self.text = mtext.Text(
307306
text=label,
308-
horizontalalignment=self.halign[self.labelpos],
309-
verticalalignment=self.valign[self.labelpos],
310-
fontproperties=self.fontproperties)
311-
if self.labelcolor is not None:
312-
self.text.set_color(self.labelcolor)
307+
horizontalalignment=self.halign[self._labelpos],
308+
verticalalignment=self.valign[self._labelpos],
309+
fontproperties=self.fontproperties,
310+
color=labelcolor)
313311
self._dpi_at_last_init = None
314312
self.zorder = Q.zorder + 0.1
315313

314+
def get_label_text(self):
315+
"""Return the label string."""
316+
return self.text.get_text()
317+
318+
def set_label_text(self, text):
319+
"""Set the label string."""
320+
self.text.set_text(text)
321+
self.stale = True
322+
323+
label = property(get_label_text, set_label_text, doc="The label string.")
324+
325+
def get_label_color(self):
326+
"""Return the label color."""
327+
return self.text.get_color()
328+
329+
def set_label_color(self, labelcolor):
330+
"""Set the label color."""
331+
self.text.set_color(labelcolor)
332+
self.stale = True
333+
334+
labelcolor = property(get_label_color, set_label_color, doc="The label color.")
335+
336+
def get_label_pos(self):
337+
"""Return the label position."""
338+
return self._labelpos
339+
340+
def set_label_pos(self, labelpos):
341+
"""
342+
Set the label position.
343+
344+
Parameters
345+
----------
346+
labelpos : {'N', 'S', 'E', 'W'}
347+
Position the label above, below, to the right, to the left of the
348+
arrow, respectively.
349+
"""
350+
_api.check_in_list(['N', 'S', 'E', 'W'], labelpos=labelpos)
351+
self._labelpos = labelpos
352+
self.text.set_horizontalalignment(self.halign[labelpos])
353+
self.text.set_verticalalignment(self.valign[labelpos])
354+
self._initialized = False
355+
self.stale = True
356+
357+
labelpos = property(get_label_pos, set_label_pos, doc="The label position.")
358+
316359
@property
317360
def labelsep(self):
318361
return self._labelsep_inches * self.Q.axes.figure.dpi
@@ -322,7 +365,7 @@ def _init(self):
322365
if self.Q._dpi_at_last_init != self.Q.axes.figure.dpi:
323366
self.Q._init()
324367
self._set_transform()
325-
with cbook._setattr_cm(self.Q, pivot=self.pivot[self.labelpos],
368+
with cbook._setattr_cm(self.Q, pivot=self.pivot[self._labelpos],
326369
# Hack: save and restore the Umask
327370
Umask=ma.nomask):
328371
u = self.U * np.cos(np.radians(self.angle))
@@ -348,7 +391,7 @@ def _text_shift(self):
348391
"S": (0, -self.labelsep),
349392
"E": (+self.labelsep, 0),
350393
"W": (-self.labelsep, 0),
351-
}[self.labelpos]
394+
}[self._labelpos]
352395

353396
@martist.allow_rasterization
354397
def draw(self, renderer):

lib/matplotlib/quiver.pyi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ class QuiverKey(martist.Artist):
2323
angle: float
2424
coord: Literal["axes", "figure", "data", "inches"]
2525
color: ColorType | None
26-
label: str
27-
labelpos: Literal["N", "S", "E", "W"]
28-
labelcolor: ColorType | None
2926
fontproperties: dict[str, Any]
3027
kw: dict[str, Any]
3128
text: Text
@@ -47,6 +44,15 @@ class QuiverKey(martist.Artist):
4744
fontproperties: dict[str, Any] | None = ...,
4845
**kwargs
4946
) -> None: ...
47+
def get_label_text(self) -> str: ...
48+
def set_label_text(self, text: str) -> None: ...
49+
label: str
50+
def get_label_color(self) -> ColorType | None: ...
51+
def set_label_color(self, labelcolor: ColorType | None) -> None: ...
52+
labelcolor: ColorType | None
53+
def get_label_pos(self) -> Literal["N", "S", "E", "W"]: ...
54+
def set_label_pos(self, labelpos: Literal["N", "S", "E", "W"]) -> None: ...
55+
labelpos: Literal["N", "S", "E", "W"]
5056
@property
5157
def labelsep(self) -> float: ...
5258
def set_figure(self, fig: Figure) -> None: ...

lib/matplotlib/tests/test_quiver.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ def test_quiver_with_key():
113113
fig, ax = plt.subplots()
114114
ax.margins(0.1)
115115
Q = draw_quiver(ax)
116-
ax.quiverkey(Q, 0.5, 0.95, 2,
117-
r'$2\, \mathrm{m}\, \mathrm{s}^{-1}$',
118-
angle=-10,
119-
coordinates='figure',
120-
labelpos='W',
121-
fontproperties={'weight': 'bold', 'size': 'large'})
116+
qk = ax.quiverkey(Q, 0.5, 0.95, 2, '',
117+
angle=-10, coordinates='figure',
118+
labelpos='W', labelcolor='b',
119+
fontproperties={'weight': 'bold', 'size': 'large'})
120+
qk.set_label_text(r'$2\, \mathrm{m}\, \mathrm{s}^{-1}$')
121+
qk.set_label_color('k') # Go back to default to keep same test image.
122122

123123

124124
@image_comparison(['quiver_single_test_image.png'], remove_text=True)
@@ -147,8 +147,8 @@ def test_quiver_key_pivot():
147147
ax.set_ylim(-2, 11)
148148
ax.quiverkey(q, 0.5, 1, 1, 'N', labelpos='N')
149149
ax.quiverkey(q, 1, 0.5, 1, 'E', labelpos='E')
150-
ax.quiverkey(q, 0.5, 0, 1, 'S', labelpos='S')
151-
ax.quiverkey(q, 0, 0.5, 1, 'W', labelpos='W')
150+
ax.quiverkey(q, 0.5, 0, 1, 'S').set_label_pos('S')
151+
ax.quiverkey(q, 0, 0.5, 1, 'W').set_label_pos('W')
152152

153153

154154
@image_comparison(['quiver_key_xy.png'], remove_text=True)

0 commit comments

Comments
 (0)