-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add set_XY
and set_data
to Quiver
#22407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
""" | ||
|
||
import math | ||
from numbers import Number | ||
import weakref | ||
|
||
import numpy as np | ||
|
@@ -426,19 +427,28 @@ def _parse_args(*args, caller_name='function'): | |
|
||
nr, nc = (1, U.shape[0]) if U.ndim == 1 else U.shape | ||
|
||
if X is not None: | ||
X = X.ravel() | ||
Y = Y.ravel() | ||
if len(X) == nc and len(Y) == nr: | ||
X, Y = [a.ravel() for a in np.meshgrid(X, Y)] | ||
elif len(X) != len(Y): | ||
raise ValueError('X and Y must be the same size, but ' | ||
f'X.size is {X.size} and Y.size is {Y.size}.') | ||
else: | ||
if X is None: | ||
indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) | ||
X, Y = [np.ravel(a) for a in indexgrid] | ||
# Size validation for U, V, C is left to the set_UVC method. | ||
return X, Y, U, V, C | ||
return X, Y, U, V, C, nr, nc | ||
|
||
|
||
def _process_XY(X, Y, nc, nr): | ||
X = X.ravel() | ||
Y = Y.ravel() | ||
if len(X) == nc and len(Y) == nr: | ||
X, Y = [a.ravel() for a in np.meshgrid(X, Y)] | ||
elif len(X) != len(Y): | ||
raise ValueError( | ||
'X and Y must be the same size, but ' | ||
f'X.size is {X.size} and Y.size is {Y.size}.' | ||
) | ||
return X, Y | ||
|
||
|
||
def _extract_nr_nc(U): | ||
return (1, U.shape[0]) if U.ndim == 1 else U.shape | ||
|
||
|
||
def _check_consistent_shapes(*arrays): | ||
|
@@ -451,11 +461,10 @@ class Quiver(mcollections.PolyCollection): | |
""" | ||
Specialized PolyCollection for arrows. | ||
|
||
The only API method is set_UVC(), which can be used | ||
to change the size, orientation, and color of the | ||
arrows; their locations are fixed when the class is | ||
instantiated. Possibly this method will be useful | ||
in animations. | ||
The only API methods are ``set_UVC()``, ``set_XY``, | ||
and ``set_data``, which can be used | ||
to change the size, orientation, color and locations | ||
of the arrows. | ||
|
||
Much of the work in this class is done in the draw() | ||
method so that as much information as possible is available | ||
|
@@ -479,11 +488,9 @@ def __init__(self, ax, *args, | |
%s | ||
""" | ||
self._axes = ax # The attr actually set by the Artist.axes property. | ||
X, Y, U, V, C = _parse_args(*args, caller_name='quiver()') | ||
self.X = X | ||
self.Y = Y | ||
self.XY = np.column_stack((X, Y)) | ||
self.N = len(X) | ||
X, Y, U, V, C, self._nr, self._nc = _parse_args( | ||
*args, caller_name='quiver()' | ||
) | ||
self.scale = scale | ||
self.headwidth = headwidth | ||
self.headlength = float(headlength) | ||
|
@@ -503,10 +510,14 @@ def __init__(self, ax, *args, | |
self.transform = kwargs.pop('transform', ax.transData) | ||
kwargs.setdefault('facecolors', color) | ||
kwargs.setdefault('linewidths', (0,)) | ||
super().__init__([], offsets=self.XY, offset_transform=self.transform, | ||
super().__init__([], offset_transform=self.transform, | ||
closed=False, **kwargs) | ||
self.polykw = kwargs | ||
self.set_UVC(U, V, C) | ||
|
||
self.X = self.Y = self.U = self.V = self.C = None | ||
self.set_data(X, Y, U, V, C) | ||
# self.U = self.V = self.C = None | ||
# self.set_UVC(U, V, C) | ||
self._initialized = False | ||
|
||
weak_self = weakref.ref(self) # Prevent closure over the real self. | ||
|
@@ -567,17 +578,58 @@ def draw(self, renderer): | |
self.stale = False | ||
|
||
def set_UVC(self, U, V, C=None): | ||
self.set_data(U=U, V=V, C=C) | ||
|
||
def set_XY(self, X, Y): | ||
""" | ||
Update the locations of the arrows. | ||
|
||
Parameters | ||
---------- | ||
X, Y : arraylike of float | ||
The arrow locations, any shape is valid so long | ||
as X and Y have the same size. | ||
""" | ||
self.set_data(X=X, Y=Y) | ||
|
||
def set_data(self, X=None, Y=None, U=None, V=None, C=None): | ||
""" | ||
Update the locations and/or rotation and color of the arrows. | ||
|
||
Parameters | ||
---------- | ||
X, Y : arraylike of float | ||
The arrow locations, any shape is valid so long | ||
as X and Y have the same size. | ||
U, V : ??? | ||
C : ??? | ||
Comment on lines
+604
to
+605
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note to self - this is part of the "cleaning up" |
||
""" | ||
X = self.X if X is None else X | ||
Y = self.Y if Y is None else Y | ||
if U is None or isinstance(U, Number): | ||
nr, nc = (self._nr, self._nc) | ||
else: | ||
nr, nc = _extract_nr_nc(U) | ||
X, Y = _process_XY(X, Y, nc, nr) | ||
N = len(X) | ||
|
||
# We need to ensure we have a copy, not a reference | ||
# to an array that might change before draw(). | ||
U = ma.masked_invalid(U, copy=True).ravel() | ||
V = ma.masked_invalid(V, copy=True).ravel() | ||
if C is not None: | ||
C = ma.masked_invalid(C, copy=True).ravel() | ||
U = ma.masked_invalid(self.U if U is None else U, copy=True).ravel() | ||
V = ma.masked_invalid(self.V if V is None else V, copy=True).ravel() | ||
if C is not None or self.C is not None: | ||
C = ma.masked_invalid( | ||
self.C if C is None else C, copy=True | ||
).ravel() | ||
for name, var in zip(('U', 'V', 'C'), (U, V, C)): | ||
if not (var is None or var.size == self.N or var.size == 1): | ||
raise ValueError(f'Argument {name} has a size {var.size}' | ||
f' which does not match {self.N},' | ||
' the number of arrow positions') | ||
if not (var is None or var.size == N or var.size == 1): | ||
raise ValueError( | ||
f'Argument {name} has a size {var.size}' | ||
f' which does not match {N},' | ||
' the number of arrow positions' | ||
) | ||
|
||
# now shapes are validated and we can start assigning things | ||
|
||
mask = ma.mask_or(U.mask, V.mask, copy=False, shrink=True) | ||
if C is not None: | ||
|
@@ -591,7 +643,12 @@ def set_UVC(self, U, V, C=None): | |
self.Umask = mask | ||
if C is not None: | ||
self.set_array(C) | ||
self.X = X | ||
self.Y = Y | ||
self.XY = np.column_stack([X, Y]) | ||
self.N = N | ||
self._new_UV = True | ||
self.set_offsets(self.XY) | ||
self.stale = True | ||
|
||
def _dots_per_unit(self, units): | ||
|
@@ -963,10 +1020,11 @@ def __init__(self, ax, *args, | |
kwargs['linewidth'] = 1 | ||
|
||
# Parse out the data arrays from the various configurations supported | ||
x, y, u, v, c = _parse_args(*args, caller_name='barbs()') | ||
self.x = x | ||
self.y = y | ||
xy = np.column_stack((x, y)) | ||
x, y, u, v, c, self._nr, self._nc = _parse_args( | ||
*args, caller_name='barbs()' | ||
) | ||
self.x, self.y = _process_XY(x, y, self._nr, self._nc) | ||
xy = np.column_stack([self.x, self.y]) | ||
|
||
# Make a collection | ||
barb_size = self._length ** 2 / 4 # Empirically determined | ||
|
@@ -1198,6 +1256,19 @@ def set_UVC(self, U, V, C=None): | |
self._offsets = xy | ||
self.stale = True | ||
|
||
def set_XY(self, X, Y): | ||
""" | ||
Update the locations of the arrows. | ||
|
||
Parameters | ||
---------- | ||
X, Y : arraylike of float | ||
The arrow locations, any shape is valid so long | ||
as X and Y have the same size. | ||
""" | ||
self.X, self.Y = _process_XY(X, Y, self._nr, self._nc) | ||
self.set_offsets(np.column_stack([X, Y])) | ||
|
||
def set_offsets(self, xy): | ||
""" | ||
Set the offsets for the barb polygons. This saves the offsets passed | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove "only"? (Does three count as only?)
Single back-ticks will link, right? I guess one should be consistent with either using
()
for all or none of the methods.