From cd500c79ac8303f6a12aef79d2edca1587800468 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 17 Jul 2018 11:52:19 +0200 Subject: [PATCH 1/2] Use super() instead of manually fetching supermethods for parasite axes. (This manual fetching was previously done in _get_base_axes_attr.) --- lib/mpl_toolkits/axes_grid1/parasite_axes.py | 74 +++++++------------- 1 file changed, 24 insertions(+), 50 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index bd8dd58a4e57..840b86a7de74 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -10,7 +10,7 @@ import numpy as np -class ParasiteAxesBase(object): +class ParasiteAxesBase: def get_images_artists(self): artists = {a for a in self.get_children() if a.get_visible()} @@ -21,11 +21,10 @@ def get_images_artists(self): def __init__(self, parent_axes, **kwargs): self._parent_axes = parent_axes kwargs["frameon"] = False - self._get_base_axes_attr("__init__")( - self, parent_axes.figure, parent_axes._position, **kwargs) + super().__init__(parent_axes.figure, parent_axes._position, **kwargs) def cla(self): - self._get_base_axes_attr("cla")(self) + super().cla() martist.setp(self.get_children(), visible=False) self._get_lines = self._parent_axes._get_lines @@ -45,18 +44,14 @@ def parasite_axes_class_factory(axes_class=None): if axes_class is None: axes_class = Axes - def _get_base_axes_attr(self, attrname): - return getattr(axes_class, attrname) - return type("%sParasite" % axes_class.__name__, - (ParasiteAxesBase, axes_class), - {'_get_base_axes_attr': _get_base_axes_attr}) + (ParasiteAxesBase, axes_class), {}) ParasiteAxes = parasite_axes_class_factory() -class ParasiteAxesAuxTransBase(object): +class ParasiteAxesAuxTransBase: def __init__(self, parent_axes, aux_transform, viewlim_mode=None, **kwargs): @@ -87,7 +82,6 @@ def set_viewlim_mode(self, mode): def get_viewlim_mode(self): return self._viewlim_mode - def update_viewlim(self): viewlim = self._parent_axes.viewLim.frozen() mode = self.get_viewlim_mode() @@ -100,8 +94,7 @@ def update_viewlim(self): else: raise ValueError("Unknown mode : %s" % (self._viewlim_mode,)) - - def _pcolor(self, method_name, *XYC, **kwargs): + def _pcolor(self, super_pcolor, *XYC, **kwargs): if len(XYC) == 1: C = XYC[0] ny, nx = C.shape @@ -113,29 +106,26 @@ def _pcolor(self, method_name, *XYC, **kwargs): else: X, Y, C = XYC - pcolor_routine = self._get_base_axes_attr(method_name) - if "transform" in kwargs: - mesh = pcolor_routine(self, X, Y, C, **kwargs) + mesh = super_pcolor(self, X, Y, C, **kwargs) else: orig_shape = X.shape xy = np.vstack([X.flat, Y.flat]) xyt=xy.transpose() wxy = self.transAux.transform(xyt) gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape) - mesh = pcolor_routine(self, gx, gy, C, **kwargs) + mesh = super_pcolor(self, gx, gy, C, **kwargs) mesh.set_transform(self._parent_axes.transData) return mesh def pcolormesh(self, *XYC, **kwargs): - return self._pcolor("pcolormesh", *XYC, **kwargs) + return self._pcolor(super().pcolormesh, *XYC, **kwargs) def pcolor(self, *XYC, **kwargs): - return self._pcolor("pcolor", *XYC, **kwargs) - + return self._pcolor(super().pcolor, *XYC, **kwargs) - def _contour(self, method_name, *XYCL, **kwargs): + def _contour(self, super_contour, *XYCL, **kwargs): if len(XYCL) <= 2: C = XYCL[0] @@ -150,32 +140,29 @@ def _contour(self, method_name, *XYCL, **kwargs): X, Y = XYCL[:2] CL = XYCL[2:] - contour_routine = self._get_base_axes_attr(method_name) - if "transform" in kwargs: - cont = contour_routine(self, X, Y, *CL, **kwargs) + cont = super_contour(self, X, Y, *CL, **kwargs) else: orig_shape = X.shape xy = np.vstack([X.flat, Y.flat]) xyt=xy.transpose() wxy = self.transAux.transform(xyt) gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape) - cont = contour_routine(self, gx, gy, *CL, **kwargs) + cont = super_contour(self, gx, gy, *CL, **kwargs) for c in cont.collections: c.set_transform(self._parent_axes.transData) return cont def contour(self, *XYCL, **kwargs): - return self._contour("contour", *XYCL, **kwargs) + return self._contour(super().contour, *XYCL, **kwargs) def contourf(self, *XYCL, **kwargs): - return self._contour("contourf", *XYCL, **kwargs) + return self._contour(super().contourf, *XYCL, **kwargs) def apply_aspect(self, position=None): self.update_viewlim() - self._get_base_axes_attr("apply_aspect")(self) - #ParasiteAxes.apply_aspect() + super().apply_aspect() @functools.lru_cache(None) @@ -209,10 +196,10 @@ def _get_handles(ax): return handles -class HostAxesBase(object): +class HostAxesBase: def __init__(self, *args, **kwargs): self.parasites = [] - self._get_base_axes_attr("__init__")(self, *args, **kwargs) + super().__init__(*args, **kwargs) def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=None): parasite_axes_class = parasite_axes_auxtrans_class_factory(axes_class) @@ -224,13 +211,9 @@ def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=None): return ax2 def _get_legend_handles(self, legend_handler_map=None): - # don't use this! - Axes_get_legend_handles = self._get_base_axes_attr("_get_legend_handles") - all_handles = list(Axes_get_legend_handles(self, legend_handler_map)) - + all_handles = super()._get_legend_handles() for ax in self.parasites: all_handles.extend(ax._get_legend_handles(legend_handler_map)) - return all_handles def draw(self, renderer): @@ -257,14 +240,14 @@ def draw(self, renderer): self.images.extend(images) self.artists.extend(artists) - self._get_base_axes_attr("draw")(self, renderer) + super().draw(renderer) self.artists = orig_artists self.images = orig_images def cla(self): for ax in self.parasites: ax.cla() - self._get_base_axes_attr("cla")(self) + super().cla() def twinx(self, axes_class=None): """ @@ -361,15 +344,10 @@ def _remove_method(h): return ax2 def get_tightbbox(self, renderer, call_axes_locator=True): - bbs = [ax.get_tightbbox(renderer, call_axes_locator) for ax in self.parasites] - get_tightbbox = self._get_base_axes_attr("get_tightbbox") - bbs.append(get_tightbbox(self, renderer, call_axes_locator)) - - _bbox = Bbox.union([b for b in bbs if b.width!=0 or b.height!=0]) - - return _bbox + bbs.append(super().get_tightbbox(renderer, call_axes_locator)) + return Bbox.union([b for b in bbs if b.width!=0 or b.height!=0]) @functools.lru_cache(None) @@ -380,13 +358,9 @@ def host_axes_class_factory(axes_class=None): def _get_base_axes(self): return axes_class - def _get_base_axes_attr(self, attrname): - return getattr(axes_class, attrname) - return type("%sHostAxes" % axes_class.__name__, (HostAxesBase, axes_class), - {'_get_base_axes_attr': _get_base_axes_attr, - '_get_base_axes': _get_base_axes}) + {'_get_base_axes': _get_base_axes}) def host_subplot_class_factory(axes_class): From 33545848d410972a2c993cfef4ee0deae5ebdafa Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 17 Jul 2018 11:59:39 +0200 Subject: [PATCH 2/2] Style-clean parasite axes. --- .flake8 | 1 - lib/mpl_toolkits/axes_grid1/parasite_axes.py | 45 ++++++++------------ 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/.flake8 b/.flake8 index c2034704a535..0884c4406392 100644 --- a/.flake8 +++ b/.flake8 @@ -70,7 +70,6 @@ per-file-ignores = mpl_toolkits/axes_grid1/colorbar.py: E225, E231, E261, E262, E302, E303, E501, E701 mpl_toolkits/axes_grid1/inset_locator.py: E501 mpl_toolkits/axes_grid1/mpl_axes.py: E303, E501 - mpl_toolkits/axes_grid1/parasite_axes.py: E225, E231, E302, E303, E501 mpl_toolkits/axisartist/angle_helper.py: E201, E203, E221, E222, E225, E231, E251, E261, E262, E302, E303, E501 mpl_toolkits/axisartist/axis_artist.py: E201, E202, E221, E225, E228, E231, E251, E261, E262, E302, E303, E402, E501, E701, E711 mpl_toolkits/axisartist/axisline_style.py: E231, E261, E262, E302, E303 diff --git a/lib/mpl_toolkits/axes_grid1/parasite_axes.py b/lib/mpl_toolkits/axes_grid1/parasite_axes.py index 840b86a7de74..051d661a800e 100644 --- a/lib/mpl_toolkits/axes_grid1/parasite_axes.py +++ b/lib/mpl_toolkits/axes_grid1/parasite_axes.py @@ -75,7 +75,7 @@ def _set_lim_and_transforms(self): def set_viewlim_mode(self, mode): if mode not in [None, "equal", "transform"]: - raise ValueError("Unknown mode : %s" % (mode,)) + raise ValueError("Unknown mode: %s" % (mode,)) else: self._viewlim_mode = mode @@ -90,17 +90,18 @@ def update_viewlim(self): elif mode == "equal": self.axes.viewLim.set(viewlim) elif mode == "transform": - self.axes.viewLim.set(viewlim.transformed(self.transAux.inverted())) + self.axes.viewLim.set( + viewlim.transformed(self.transAux.inverted())) else: - raise ValueError("Unknown mode : %s" % (self._viewlim_mode,)) + raise ValueError("Unknown mode: %s" % (self._viewlim_mode,)) def _pcolor(self, super_pcolor, *XYC, **kwargs): if len(XYC) == 1: C = XYC[0] ny, nx = C.shape - gx = np.arange(-0.5, nx, 1.) - gy = np.arange(-0.5, ny, 1.) + gx = np.arange(-0.5, nx) + gy = np.arange(-0.5, ny) X, Y = np.meshgrid(gx, gy) else: @@ -110,10 +111,10 @@ def _pcolor(self, super_pcolor, *XYC, **kwargs): mesh = super_pcolor(self, X, Y, C, **kwargs) else: orig_shape = X.shape - xy = np.vstack([X.flat, Y.flat]) - xyt=xy.transpose() + xyt = np.column_stack([X.flat, Y.flat]) wxy = self.transAux.transform(xyt) - gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape) + gx = wxy[:, 0].reshape(orig_shape) + gy = wxy[:, 1].reshape(orig_shape) mesh = super_pcolor(self, gx, gy, C, **kwargs) mesh.set_transform(self._parent_axes.transData) @@ -131,10 +132,10 @@ def _contour(self, super_contour, *XYCL, **kwargs): C = XYCL[0] ny, nx = C.shape - gx = np.arange(0., nx, 1.) - gy = np.arange(0., ny, 1.) + gx = np.arange(0., nx) + gy = np.arange(0., ny) - X,Y = np.meshgrid(gx, gy) + X, Y = np.meshgrid(gx, gy) CL = XYCL else: X, Y = XYCL[:2] @@ -144,10 +145,10 @@ def _contour(self, super_contour, *XYCL, **kwargs): cont = super_contour(self, X, Y, *CL, **kwargs) else: orig_shape = X.shape - xy = np.vstack([X.flat, Y.flat]) - xyt=xy.transpose() + xyt = np.column_stack([X.flat, Y.flat]) wxy = self.transAux.transform(xyt) - gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape) + gx = wxy[:, 0].reshape(orig_shape) + gy = wxy[:, 1].reshape(orig_shape) cont = super_contour(self, gx, gy, *CL, **kwargs) for c in cont.collections: c.set_transform(self._parent_axes.transData) @@ -183,19 +184,6 @@ def parasite_axes_auxtrans_class_factory(axes_class=None): axes_class=ParasiteAxes) -def _get_handles(ax): - handles = ax.lines[:] - handles.extend(ax.patches) - handles.extend([c for c in ax.collections - if isinstance(c, mcoll.LineCollection)]) - handles.extend([c for c in ax.collections - if isinstance(c, mcoll.RegularPolyCollection)]) - handles.extend([c for c in ax.collections - if isinstance(c, mcoll.CircleCollection)]) - - return handles - - class HostAxesBase: def __init__(self, *args, **kwargs): self.parasites = [] @@ -347,7 +335,7 @@ def get_tightbbox(self, renderer, call_axes_locator=True): bbs = [ax.get_tightbbox(renderer, call_axes_locator) for ax in self.parasites] bbs.append(super().get_tightbbox(renderer, call_axes_locator)) - return Bbox.union([b for b in bbs if b.width!=0 or b.height!=0]) + return Bbox.union([b for b in bbs if b.width != 0 or b.height != 0]) @functools.lru_cache(None) @@ -395,6 +383,7 @@ def host_axes(*args, axes_class=None, figure=None, **kwargs): plt.draw_if_interactive() return ax + def host_subplot(*args, axes_class=None, figure=None, **kwargs): """ Create a subplot that can act as a host to parasitic axes. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy