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

Skip to content

Commit ef98721

Browse files
committed
Let quiver retrieve X, Y from base PolyCollection.get_offsets().
... instead of additionally storing copies locally in `self.X, self.Y, self.XY` (for quiver), `self.x, self.y` (for barbs). Then there's no reason not to also suggest set_offsets in quiver(), like it's already done in barbs().
1 parent a95775e commit ef98721

File tree

1 file changed

+36
-41
lines changed

1 file changed

+36
-41
lines changed

lib/matplotlib/quiver.py

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -445,22 +445,20 @@ def _check_consistent_shapes(*arrays):
445445

446446
class Quiver(mcollections.PolyCollection):
447447
"""
448-
Specialized PolyCollection for arrows.
449-
450-
The only API method is set_UVC(), which can be used
451-
to change the size, orientation, and color of the
452-
arrows; their locations are fixed when the class is
453-
instantiated. Possibly this method will be useful
454-
in animations.
455-
456-
Much of the work in this class is done in the draw()
457-
method so that as much information as possible is available
458-
about the plot. In subsequent draw() calls, recalculation
459-
is limited to things that might have changed, so there
460-
should be no performance penalty from putting the calculations
461-
in the draw() method.
448+
Specialized `.PolyCollection` for arrows.
449+
450+
The only API method is `set_UVC`, which can be used to change the size,
451+
orientation, and color of the arrows. Locations are changed using the
452+
(inherited) `.PolyCollection.set_offsets` method. This method may be
453+
useful in animations.
462454
"""
463455

456+
# Much of the work in this class is done in the draw() method so that as
457+
# much information as possible is available about the plot. In subsequent
458+
# draw() calls, recalculation is limited to things that might have changed,
459+
# so there should be no performance penalty from putting the calculations
460+
# in the draw() method.
461+
464462
_PIVOT_VALS = ('tail', 'middle', 'tip')
465463

466464
@_docstring.Substitution(_quiver_doc)
@@ -476,10 +474,6 @@ def __init__(self, ax, *args,
476474
"""
477475
self._axes = ax # The attr actually set by the Artist.axes property.
478476
X, Y, U, V, C = _parse_args(*args, caller_name='quiver')
479-
self.X = X
480-
self.Y = Y
481-
self.XY = np.column_stack((X, Y))
482-
self.N = len(X)
483477
self.scale = scale
484478
self.headwidth = headwidth
485479
self.headlength = float(headlength)
@@ -499,12 +493,18 @@ def __init__(self, ax, *args,
499493
self.transform = kwargs.pop('transform', ax.transData)
500494
kwargs.setdefault('facecolors', color)
501495
kwargs.setdefault('linewidths', (0,))
502-
super().__init__([], offsets=self.XY, offset_transform=self.transform,
496+
super().__init__([], offsets=np.column_stack([X, Y]),
497+
offset_transform=self.transform,
503498
closed=False, **kwargs)
504499
self.polykw = kwargs
505500
self.set_UVC(U, V, C)
506501
self._dpi_at_last_init = None
507502

503+
XY = property(lambda self: self.get_offsets())
504+
X = property(lambda self: self.get_offsets()[:, 0])
505+
Y = property(lambda self: self.get_offsets()[:, 1])
506+
N = property(lambda self: len(self.get_offsets()))
507+
508508
def _init(self):
509509
"""
510510
Initialization delayed until first draw;
@@ -675,16 +675,14 @@ def _h_arrows(self, length):
675675
# length = np.minimum(length, 2 ** 16)
676676
np.clip(length, 0, 2 ** 16, out=length)
677677
# x, y: normal horizontal arrow
678-
x = np.array([0, -self.headaxislength,
679-
-self.headlength, 0],
680-
np.float64)
678+
x = np.array([0, -self.headaxislength, -self.headlength, 0], float)
681679
x = x + np.array([0, 1, 1, 1]) * length
682-
y = 0.5 * np.array([1, 1, self.headwidth, 0], np.float64)
680+
y = 0.5 * np.array([1, 1, self.headwidth, 0], float)
683681
y = np.repeat(y[np.newaxis, :], N, axis=0)
684682
# x0, y0: arrow without shaft, for short vectors
685683
x0 = np.array([0, minsh - self.headaxislength,
686-
minsh - self.headlength, minsh], np.float64)
687-
y0 = 0.5 * np.array([1, 1, self.headwidth, 0], np.float64)
684+
minsh - self.headlength, minsh], float)
685+
y0 = 0.5 * np.array([1, 1, self.headwidth, 0], float)
688686
ii = [0, 1, 2, 3, 2, 1, 0, 0]
689687
X = x[:, ii]
690688
Y = y[:, ii]
@@ -866,22 +864,18 @@ def _h_arrows(self, length):
866864

867865
class Barbs(mcollections.PolyCollection):
868866
"""
869-
Specialized PolyCollection for barbs.
870-
871-
The only API method is :meth:`set_UVC`, which can be used to
872-
change the size, orientation, and color of the arrows. Locations
873-
are changed using the :meth:`set_offsets` collection method.
874-
Possibly this method will be useful in animations.
867+
Specialized `.PolyCollection` for barbs.
875868
876-
There is one internal function :meth:`_find_tails` which finds
877-
exactly what should be put on the barb given the vector magnitude.
878-
From there :meth:`_make_barbs` is used to find the vertices of the
879-
polygon to represent the barb based on this information.
869+
The only API method is `set_UVC`, which can be used to change the size,
870+
orientation, and color of the arrows. Locations are changed using the
871+
(inherited) `.PolyCollection.set_offsets` method. This method may be
872+
useful in animations.
880873
"""
881874

882-
# This may be an abuse of polygons here to render what is essentially maybe
883-
# 1 triangle and a series of lines. It works fine as far as I can tell
884-
# however.
875+
# There is one internal function :meth:`_find_tails` which finds
876+
# exactly what should be put on the barb given the vector magnitude.
877+
# From there :meth:`_make_barbs` is used to find the vertices of the
878+
# polygon to represent the barb based on this information.
885879

886880
@_docstring.interpd
887881
def __init__(self, ax, *args,
@@ -903,7 +897,7 @@ def __init__(self, ax, *args,
903897
self._pivot = pivot
904898
self._length = length
905899

906-
# Flagcolor and barbcolor provide convenience parameters for
900+
# flagcolor and barbcolor provide convenience parameters for
907901
# setting the facecolor and edgecolor, respectively, of the barb
908902
# polygon. We also work here to make the flag the same color as the
909903
# rest of the barb by default
@@ -928,8 +922,6 @@ def __init__(self, ax, *args,
928922

929923
# Parse out the data arrays from the various configurations supported
930924
x, y, u, v, c = _parse_args(*args, caller_name='barbs')
931-
self.x = x
932-
self.y = y
933925
xy = np.column_stack((x, y))
934926

935927
# Make a collection
@@ -940,6 +932,9 @@ def __init__(self, ax, *args,
940932

941933
self.set_UVC(u, v, c)
942934

935+
x = property(lambda self: self.get_offsets()[:, 0])
936+
y = property(lambda self: self.get_offsets()[:, 1])
937+
943938
def _find_tails(self, mag, rounding=True, half=5, full=10, flag=50):
944939
"""
945940
Find how many of each of the tail pieces is necessary.

0 commit comments

Comments
 (0)