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

Skip to content

Commit a3c5032

Browse files
committed
Move QuiverKey position info onto its label Text
Additionally, add getters/setters for these properties, and use an `offset_copy` Transform to implement `labelsep`. This makes one less thing depending on the DPI change signal.
1 parent cb45873 commit a3c5032

File tree

5 files changed

+105
-15
lines changed

5 files changed

+105
-15
lines changed

doc/api/next_api_changes/behavior/18794-ES.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ effect on the final result.
99
Now all such properties have getters and setters, and may be modified after
1010
creation:
1111

12+
- `.QuiverKey.X` -> `.QuiverKey.get_x` / `.QuiverKey.set_x` /
13+
`.QuiverKey.get_position` / `.QuiverKey.set_position`
14+
- `.QuiverKey.Y` -> `.QuiverKey.get_y` / `.QuiverKey.set_y` /
15+
`.QuiverKey.get_position` / `.QuiverKey.set_position`
1216
- `.QuiverKey.label` -> `.QuiverKey.get_label` / `.QuiverKey.set_label`
1317
- `.QuiverKey.labelcolor` -> `.QuiverKey.get_labelcolor` /
1418
`.QuiverKey.set_labelcolor`
1519
- `.QuiverKey.labelpos` -> `.QuiverKey.get_labelpos` /
1620
`.QuiverKey.set_labelpos`
21+
- `.QuiverKey.labelsep` is now read-only as it used a different unit (pixels)
22+
than the constructor (inches), and was automatically overwritten;
23+
`.QuiverKey.get_labelsep` and `.QuiverKey.set_labelsep` have been added which
24+
use inches

doc/users/next_whats_new/quiverkey.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ effect on the final result.
99
Now all such properties have getters and setters, and may be modified after
1010
creation:
1111

12+
- `.QuiverKey.X` -> `.QuiverKey.get_x` / `.QuiverKey.set_x` /
13+
`.QuiverKey.get_position` / `.QuiverKey.set_position`
14+
- `.QuiverKey.Y` -> `.QuiverKey.get_y` / `.QuiverKey.set_y` /
15+
`.QuiverKey.get_position` / `.QuiverKey.set_position`
1216
- `.QuiverKey.label` -> `.QuiverKey.get_label` / `.QuiverKey.set_label`
1317
- `.QuiverKey.labelcolor` -> `.QuiverKey.get_labelcolor` /
1418
`.QuiverKey.set_labelcolor`
1519
- `.QuiverKey.labelpos` -> `.QuiverKey.get_labelpos` /
1620
`.QuiverKey.set_labelpos`
21+
- `.QuiverKey.labelsep` is now read-only as it used a different unit (pixels)
22+
than the constructor (inches), and was automatically overwritten;
23+
`.QuiverKey.get_labelsep` and `.QuiverKey.set_labelsep` have been added which
24+
use inches

lib/matplotlib/quiver.py

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,6 @@ def __init__(self, Q, X, Y, U, label,
290290
"""
291291
super().__init__()
292292
self.Q = Q
293-
self.X = X
294-
self.Y = Y
295293
self.U = U
296294
self.angle = angle
297295
self.coord = coordinates
@@ -303,14 +301,66 @@ def __init__(self, Q, X, Y, U, label,
303301
self.fontproperties = fontproperties or dict()
304302
self.kw = kwargs
305303
self.text = mtext.Text(
306-
text=label,
304+
x=X, y=Y, text=label,
307305
horizontalalignment=self.halign[self._labelpos],
308306
verticalalignment=self.valign[self._labelpos],
309307
fontproperties=self.fontproperties,
310308
color=labelcolor)
311309
self._dpi_at_last_init = None
312310
self.zorder = Q.zorder + 0.1
313311

312+
def get_x(self):
313+
"""Return the *x* position of the QuiverKey."""
314+
return self.text.get_position()[0]
315+
316+
def set_x(self, x):
317+
"""
318+
Set the *x* position of the QuiverKey.
319+
320+
Parameters
321+
----------
322+
x : float
323+
The *x* location of the key.
324+
"""
325+
self.text.set_x(x)
326+
self.stale = True
327+
328+
X = property(get_x, set_x)
329+
330+
def get_y(self):
331+
"""Return the *y* position of the QuiverKey."""
332+
return self.text.get_position()[1]
333+
334+
def set_y(self, y):
335+
"""
336+
Set the *y* position of the QuiverKey.
337+
338+
Parameters
339+
----------
340+
y : float
341+
The *y* location of the key.
342+
"""
343+
self.text.set_y(y)
344+
self.stale = True
345+
346+
Y = property(get_y, set_y)
347+
348+
def get_position(self):
349+
"""Return the (x, y) position of the QuiverKey."""
350+
return self.text.get_position()
351+
352+
def set_position(self, xy):
353+
"""
354+
Set the position of the QuiverKey.
355+
356+
Parameters
357+
----------
358+
xy : (float, float)
359+
The (*x*, *y*) position of the QuiverKey.
360+
"""
361+
self.text.set_position(xy)
362+
self.stale = True
363+
314364
def get_label_text(self):
315365
"""Return the label string."""
316366
return self.text.get_text()
@@ -351,13 +401,25 @@ def set_label_pos(self, labelpos):
351401
self._labelpos = labelpos
352402
self.text.set_horizontalalignment(self.halign[labelpos])
353403
self.text.set_verticalalignment(self.valign[labelpos])
404+
self._update_text_transform()
354405
self._initialized = False
355406
self.stale = True
356407

357408
labelpos = property(get_label_pos, set_label_pos, doc="The label position.")
358409

410+
def get_labelsep(self):
411+
"""Return the distance between the arrow and label in inches."""
412+
return self._labelsep_inches
413+
414+
def set_labelsep(self, labelsep):
415+
"""Set the distance between the arrow and label in inches."""
416+
self._labelsep_inches = labelsep
417+
self._update_text_transform()
418+
self.stale = True
419+
359420
@property
360421
def labelsep(self):
422+
"""Return the distance between the arrow and label in pixels."""
361423
return self._labelsep_inches * self.Q.axes.figure.dpi
362424

363425
def _init(self):
@@ -385,20 +447,21 @@ def _init(self):
385447
self.vector.set_figure(self.get_figure())
386448
self._dpi_at_last_init = self.Q.axes.figure.dpi
387449

388-
def _text_shift(self):
389-
return {
390-
"N": (0, +self.labelsep),
391-
"S": (0, -self.labelsep),
392-
"E": (+self.labelsep, 0),
393-
"W": (-self.labelsep, 0),
450+
def _update_text_transform(self):
451+
x, y = {
452+
"N": (0, +self._labelsep_inches),
453+
"S": (0, -self._labelsep_inches),
454+
"E": (+self._labelsep_inches, 0),
455+
"W": (-self._labelsep_inches, 0),
394456
}[self._labelpos]
457+
self.text.set_transform(
458+
transforms.offset_copy(self.get_transform(), self.figure, x=x, y=y))
395459

396460
@martist.allow_rasterization
397461
def draw(self, renderer):
398462
self._init()
399463
self.vector.draw(renderer)
400-
pos = self.get_transform().transform((self.X, self.Y))
401-
self.text.set_position(pos + self._text_shift())
464+
self._update_text_transform()
402465
self.text.draw(renderer)
403466
self.stale = False
404467

lib/matplotlib/quiver.pyi

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class QuiverKey(martist.Artist):
1717
valign: dict[Literal["N", "S", "E", "W"], Literal["top", "center", "bottom"]]
1818
pivot: dict[Literal["N", "S", "E", "W"], Literal["middle", "tip", "tail"]]
1919
Q: Quiver
20-
X: float
21-
Y: float
2220
U: float
2321
angle: float
2422
coord: Literal["axes", "figure", "data", "inches"]
@@ -44,6 +42,14 @@ class QuiverKey(martist.Artist):
4442
fontproperties: dict[str, Any] | None = ...,
4543
**kwargs
4644
) -> None: ...
45+
def get_x(self) -> float: ...
46+
def set_x(self, x: float) -> None: ...
47+
X: float
48+
def get_y(self) -> float: ...
49+
def set_y(self, y: float) -> None: ...
50+
Y: float
51+
def get_position(self) -> tuple[float, float]: ...
52+
def set_position(self, xy: tuple[float, float]) -> None: ...
4753
def get_label_text(self) -> str: ...
4854
def set_label_text(self, text: str) -> None: ...
4955
label: str
@@ -53,6 +59,8 @@ class QuiverKey(martist.Artist):
5359
def get_label_pos(self) -> Literal["N", "S", "E", "W"]: ...
5460
def set_label_pos(self, labelpos: Literal["N", "S", "E", "W"]) -> None: ...
5561
labelpos: Literal["N", "S", "E", "W"]
62+
def get_labelsep(self) -> float: ...
63+
def set_labelsep(self, labelsep: float) -> None: ...
5664
@property
5765
def labelsep(self) -> float: ...
5866
def set_figure(self, fig: Figure) -> None: ...

lib/matplotlib/tests/test_quiver.py

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

123126

0 commit comments

Comments
 (0)