8000 Add `set_offsets` to `quiver` and make the attribute (`N` and `XY`) p… · matplotlib/matplotlib@10adf0b · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 10adf0b

Browse files
committed
Add set_offsets to quiver and make the attribute (N and XY) properties to avoid inconsistent state of quiver
1 parent b268585 commit 10adf0b

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/matplotlib/quiver.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ class Quiver(mcollections.PolyCollection):
447447
The API methods are set_UVC(), set_U(), set_V() and set_C(), which
448448
can be used to change the size, orientation, and color of the
449449
arrows; their locations are fixed when the class is
450-
instantiated. Possibly this method will be useful
450+
instantiated. Possibly these methods will be useful
451451
in animations.
452452
453453
Much of the work in this class is done in the draw()
@@ -475,8 +475,6 @@ def __init__(self, ax, *args,
475475
X, Y, U, V, C = _parse_args(*args, caller_name='quiver')
476476
self.X = X
477477
self.Y = Y
478-
self.XY = np.column_stack((X, Y))
479-
self.N = len(X)
480478
self.scale = scale
481479
self.headwidth = headwidth
482480
self.headlength = float(headlength)
@@ -523,6 +521,14 @@ def _init(self):
523521

524522
self._dpi_at_last_init = self.axes.figure.dpi
525523< 8000 code class="diff-text syntax-highlighted-line">

524+
@property
525+
def N(self):
526+
return len(self.X)
527+
528+
@property
529+
def XY(self):
530+
return np.column_stack((self.X, self.Y))
531+
526532
def get_datalim(self, transData):
527533
trans = self.get_transform()
528534
offset_trf = self.get_offset_transform()
@@ -588,6 +594,7 @@ def set_UVC(self, U, V, C=None):
588594
The size must the same as the existing U, V or be one.
589595
C : array-like or None, optional
590596
The arrow colors. The default is None.
597+
The size must the same as the existing U, V or be one.
591598
"""
592599
if U is None:
593600
U = self.U
@@ -619,6 +626,19 @@ def set_UVC(self, U, V, C=None):
619626
self.set_array(C)
620627
self.stale = True
621628

629+
def set_offsets(self, xy):
630+
"""
631+
Set the offsets for the arrows. This saves the offsets passed
632+
in and masks them as appropriate for the existing X/Y data.
633+
634+
Parameters
635+
----------
636+
xy : sequence of pairs of floats
637+
"""
638+
self.X, self.Y = xy[:, 0], xy[:, 1]
639+
super().set_offsets(xy)
640+
self.stale = True
641+
622642
def _dots_per_unit(self, units):
623643
"""Return a scale factor for converting from units to pixels."""
624644
bb = self.axes.bbox

lib/matplotlib/quiver.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ class QuiverKey(martist.Artist):
5454
class Quiver(mcollections.PolyCollection):
5555
X: ArrayLike
5656
Y: ArrayLike
57-
XY: ArrayLike
5857
U: ArrayLike
5958
V: ArrayLike
6059
Umask: ArrayLike
61-
N: int
6260
scale: float | None
6361
headwidth: float
6462
headlength: float
@@ -121,13 +119,18 @@ class Quiver(mcollections.PolyCollection):
121119
pivot: Literal["tail", "mid", "middle", "tip"] = ...,
122120
**kwargs
123121
) -> None: ...
122+
@property
123+
def N(self) -> int: ...
124+
@property
125+
def XY(self) -> ArrayLike: ...
124126
def get_datalim(self, transData: Transform) -> Bbox: ...
125127
def set_U(self, U: ArrayLike) -> None: ...
126128
def set_V(self, V: ArrayLike) -> None: ...
127129
def set_C(self, C: ArrayLike) -> None: ...
128130
def set_UVC(
129131
self, U: ArrayLike | None, V: ArrayLike | None, C: ArrayLike | None = ...
130132
) -> None: ...
133+
def set_offsets(self, xy: ArrayLike) -> None: ...
131134

132135
class Barbs(mcollections.PolyCollection):
133136
sizes: dict[str, float]

lib/matplotlib/tests/test_collections.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ def test_quiver_offsets():
376376
qc.set_offsets(new_offsets)
377377

378378
np.testing.assert_allclose(qc.get_offsets(), new_offsets)
379+
np.testing.assert_allclose(qc.X, new_offsets[::, 0])
380+
np.testing.assert_allclose(qc.Y, new_offsets[::, 1])
381+
np.testing.assert_allclose(qc.XY, new_offsets)
379382

380383

381384
def test_quiver_UVC():

0 commit comments

Comments
 (0)
0