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

Skip to content

Commit 61bd7d2

Browse files
committed
Add set_offsets to quiver and make the attribute (N and XY) properties to avoid inconsistent state of quiver
1 parent cfe71c1 commit 61bd7d2

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
@@ -449,7 +449,7 @@ class Quiver(mcollections.PolyCollection):
449449
The API methods are set_UVC(), set_U(), set_V() and set_C(), which
450450
can be used to change the size, orientation, and color of the
451451
arrows; their locations are fixed when the class is
452-
instantiated. Possibly this method will be useful
452+
instantiated. Possibly these methods will be useful
453453
in animations.
454454
455455
Much of the work in this class is done in the draw()
@@ -477,8 +477,6 @@ def __init__(self, ax, *args,
477477
X, Y, U, V, C = _parse_args(*args, caller_name='quiver')
478478
self.X = X
479479
self.Y = Y
480-
self.XY = np.column_stack((X, Y))
481-
self.N = len(X)
482480
self.scale = scale
483481
self.headwidth = headwidth
484482
self.headlength = float(headlength)
@@ -525,6 +523,14 @@ def _init(self):
525523

526524
self._dpi_at_last_init = self.axes.figure.dpi
527525

526+
@property
527+
def N(self):
528+
return len(self.X)
529+
530+
@property
531+
def XY(self):
532+
return np.column_stack((self.X, self.Y))
533+
528534
def get_datalim(self, transData):
529535
trans = self.get_transform()
530536
offset_trf = self.get_offset_transform()
@@ -590,6 +596,7 @@ def set_UVC(self, U, V, C=None):
590596
The size must the same as the existing U, V or be one.
591597
C : array-like or None, optional
592598
The arrow colors. The default is None.
599+
The size must the same as the existing U, V or be one.
593600
"""
594601
if U is None:
595602
U = self.U
@@ -621,6 +628,19 @@ def set_UVC(self, U, V, C=None):
621628
self.set_array(C)
622629
self.stale = True
623630

631+
def set_offsets(self, xy):
632+
"""
633+
Set the offsets for the arrows. This saves the offsets passed
634+
in and masks them as appropriate for the existing X/Y data.
635+
636+
Parameters
637+
----------
638+
xy : sequence of pairs of floats
639+
"""
640+
self.X, self.Y = xy[:, 0], xy[:, 1]
641+
super().set_offsets(xy)
642+
self.stale = True
643+
624644
def _dots_per_unit(self, units):
625645
"""Return a scale factor for converting from units to pixels."""
626646
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
@property
132135
def quiver_doc(self) -> str: ...
133136

lib/matplotlib/tests/test_collections.py

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

368368
np.testing.assert_allclose(qc.get_offsets(), new_offsets)
369+
np.testing.assert_allclose(qc.X, new_offsets[::, 0])
370+
np.testing.assert_allclose(qc.Y, new_offsets[::, 1])
371+
np.testing.assert_allclose(qc.XY, new_offsets)
369372

370373

371374
def test_quiver_UVC():

0 commit comments

Comments
 (0)