Skip to content

Commit

Permalink
Deprecate Quiver.ax in favor of Quiver.axes.
Browse files Browse the repository at this point in the history
For consistency with other artists.  In particular, Colorbar.remove
assumes that the `.axes` attribute exists.

Also a smattering of cleanups.
  • Loading branch information
anntzer committed Jan 9, 2020
1 parent 860803d commit 9f5e6c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 51 deletions.
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations.rst
Expand Up @@ -135,3 +135,8 @@ access the transform classes from the :mod:`.scale` module.
``TexManager.cachedir``
~~~~~~~~~~~~~~~~~~~~~~~
Use `matplotlib.get_cachedir()` instead.

``Quiver.ax``
~~~~~~~~~~~~~
This property is deprecated in favor of ``Quiver.axes``, for consistency with
other artists.
88 changes: 37 additions & 51 deletions lib/matplotlib/quiver.py
Expand Up @@ -259,7 +259,7 @@ def __init__(self, Q, X, Y, U, label,
self.color = color
self.label = label
self._labelsep_inches = labelsep
self.labelsep = (self._labelsep_inches * Q.ax.figure.dpi)
self.labelsep = (self._labelsep_inches * Q.axes.figure.dpi)

# try to prevent closure over the real self
weak_self = weakref.ref(self)
Expand All @@ -272,8 +272,8 @@ def on_dpi_change(fig):
# the start of draw.
self_weakref._initialized = False

self._cid = Q.ax.figure.callbacks.connect('dpi_changed',
on_dpi_change)
self._cid = Q.axes.figure.callbacks.connect(
'dpi_changed', on_dpi_change)

self.labelpos = labelpos
self.labelcolor = labelcolor
Expand All @@ -293,13 +293,10 @@ def on_dpi_change(fig):
self.zorder = Q.zorder + 0.1

def remove(self):
"""
Overload the remove method
"""
self.Q.ax.figure.callbacks.disconnect(self._cid)
# docstring inherited
self.Q.axes.figure.callbacks.disconnect(self._cid)
self._cid = None
# pass the remove call up the stack
martist.Artist.remove(self)
super().remove() # pass the remove call up the stack

def _init(self):
if True: # not self._initialized:
Expand Down Expand Up @@ -358,16 +355,12 @@ def draw(self, renderer):
self.stale = False

def _set_transform(self):
if self.coord == 'data':
self.set_transform(self.Q.ax.transData)
elif self.coord == 'axes':
self.set_transform(self.Q.ax.transAxes)
elif self.coord == 'figure':
self.set_transform(self.Q.ax.figure.transFigure)
elif self.coord == 'inches':
self.set_transform(self.Q.ax.figure.dpi_scale_trans)
else:
raise ValueError('unrecognized coordinates')
self.set_transform(cbook._check_getitem({
"data": self.Q.axes.transData,
"axes": self.Q.axes.transAxes,
"figure": self.Q.axes.figure.transFigure,
"inches": self.Q.axes.figure.dpi_scale_trans,
}, coordinates=self.coord))

def set_figure(self, fig):
martist.Artist.set_figure(self, fig)
Expand Down Expand Up @@ -480,7 +473,7 @@ def __init__(self, ax, *args,
by the following pyplot interface documentation:
%s
"""
self.ax = ax
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
Expand Down Expand Up @@ -513,8 +506,7 @@ def __init__(self, ax, *args,
self.set_UVC(U, V, C)
self._initialized = False

# try to prevent closure over the real self
weak_self = weakref.ref(self)
weak_self = weakref.ref(self) # Prevent closure over the real self.

def on_dpi_change(fig):
self_weakref = weak_self()
Expand All @@ -525,18 +517,17 @@ def on_dpi_change(fig):
# the start of draw.
self_weakref._initialized = False

self._cid = self.ax.figure.callbacks.connect('dpi_changed',
on_dpi_change)
self._cid = ax.figure.callbacks.connect('dpi_changed', on_dpi_change)

@cbook.deprecated("3.3", alternative="axes")
def ax(self):
return self.axes

def remove(self):
"""
Overload the remove method
"""
# disconnect the call back
self.ax.figure.callbacks.disconnect(self._cid)
# docstring inherited
self.axes.figure.callbacks.disconnect(self._cid)
self._cid = None
# pass the remove call up the stack
mcollections.PolyCollection.remove(self)
super().remove() # pass the remove call up the stack

def _init(self):
"""
Expand All @@ -547,8 +538,7 @@ def _init(self):
# available to have this work on an as-needed basis at present.
if True: # not self._initialized:
trans = self._set_transform()
ax = self.ax
self.span = trans.inverted().transform_bbox(ax.bbox).width
self.span = trans.inverted().transform_bbox(self.axes.bbox).width
if self.width is None:
sn = np.clip(math.sqrt(self.N), 8, 25)
self.width = 0.06 * self.span / sn
Expand Down Expand Up @@ -609,31 +599,30 @@ def _dots_per_unit(self, units):
"""
Return a scale factor for converting from units to pixels
"""
ax = self.ax
if units in ('x', 'y', 'xy'):
if units == 'x':
dx0 = ax.viewLim.width
dx1 = ax.bbox.width
dx0 = self.axes.viewLim.width
dx1 = self.axes.bbox.width
elif units == 'y':
dx0 = ax.viewLim.height
dx1 = ax.bbox.height
dx0 = self.axes.viewLim.height
dx1 = self.axes.bbox.height
else: # 'xy' is assumed
dxx0 = ax.viewLim.width
dxx1 = ax.bbox.width
dyy0 = ax.viewLim.height
dyy1 = ax.bbox.height
dxx0 = self.axes.viewLim.width
dxx1 = self.axes.bbox.width
dyy0 = self.axes.viewLim.height
dyy1 = self.axes.bbox.height
dx1 = np.hypot(dxx1, dyy1)
dx0 = np.hypot(dxx0, dyy0)
dx = dx1 / dx0
else:
if units == 'width':
dx = ax.bbox.width
dx = self.axes.bbox.width
elif units == 'height':
dx = ax.bbox.height
dx = self.axes.bbox.height
elif units == 'dots':
dx = 1.0
elif units == 'inches':
dx = ax.figure.dpi
dx = self.axes.figure.dpi
else:
raise ValueError('unrecognized units')
return dx
Expand All @@ -650,9 +639,9 @@ def _set_transform(self):
return trans

def _angles_lengths(self, U, V, eps=1):
xy = self.ax.transData.transform(self.XY)
xy = self.axes.transData.transform(self.XY)
uv = np.column_stack((U, V))
xyp = self.ax.transData.transform(self.XY + eps * uv)
xyp = self.axes.transData.transform(self.XY + eps * uv)
dxy = xyp - xy
angles = np.arctan2(dxy[:, 1], dxy[:, 0])
lengths = np.hypot(*dxy.T) / eps
Expand All @@ -670,7 +659,7 @@ def _make_verts(self, U, V, angles):
# Calculate eps based on the extents of the plot
# so that we don't end up with roundoff error from
# adding a small number to a large.
eps = np.abs(self.ax.dataLim.extents).max() * 0.001
eps = np.abs(self.axes.dataLim.extents).max() * 0.001
angles, lengths = self._angles_lengths(U, V, eps=eps)
if str_angles and self.scale_units == 'xy':
a = lengths
Expand Down Expand Up @@ -806,7 +795,6 @@ def _h_arrows(self, length):
: / \ \ \
: ------------------------------
The largest increment is given by a triangle (or "flag"). After those
come full lines (barbs). The smallest increment is a half line. There
is only, of course, ever at most 1 half line. If the magnitude is
Expand All @@ -818,8 +806,6 @@ def _h_arrows(self, length):
See also https://en.wikipedia.org/wiki/Wind_barb.
Parameters
----------
X, Y : 1D or 2D array-like, optional
Expand Down

0 comments on commit 9f5e6c5

Please sign in to comment.