Skip to content

Commit 8b46f31

Browse files
authored
Merge pull request #11678 from anntzer/parasite-super
Use super() instead of manually fetching supermethods for parasite axes.
2 parents c85f821 + 3354584 commit 8b46f31

File tree

2 files changed

+40
-78
lines changed

2 files changed

+40
-78
lines changed

.flake8

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ per-file-ignores =
7070
mpl_toolkits/axes_grid1/colorbar.py: E225, E231, E261, E262, E302, E303, E501, E701
7171
mpl_toolkits/axes_grid1/inset_locator.py: E501
7272
mpl_toolkits/axes_grid1/mpl_axes.py: E303, E501
73-
mpl_toolkits/axes_grid1/parasite_axes.py: E225, E231, E302, E303, E501
7473
mpl_toolkits/axisartist/angle_helper.py: E201, E203, E221, E222, E225, E231, E251, E261, E262, E302, E303, E501
7574
mpl_toolkits/axisartist/axis_artist.py: E201, E202, E221, E225, E228, E231, E251, E261, E262, E302, E303, E402, E501, E701, E711
7675
mpl_toolkits/axisartist/axisline_style.py: E231, E261, E262, E302, E303

lib/mpl_toolkits/axes_grid1/parasite_axes.py

+40-77
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111

1212

13-
class ParasiteAxesBase(object):
13+
class ParasiteAxesBase:
1414

1515
def get_images_artists(self):
1616
artists = {a for a in self.get_children() if a.get_visible()}
@@ -21,11 +21,10 @@ def get_images_artists(self):
2121
def __init__(self, parent_axes, **kwargs):
2222
self._parent_axes = parent_axes
2323
kwargs["frameon"] = False
24-
self._get_base_axes_attr("__init__")(
25-
self, parent_axes.figure, parent_axes._position, **kwargs)
24+
super().__init__(parent_axes.figure, parent_axes._position, **kwargs)
2625

2726
def cla(self):
28-
self._get_base_axes_attr("cla")(self)
27+
super().cla()
2928

3029
martist.setp(self.get_children(), visible=False)
3130
self._get_lines = self._parent_axes._get_lines
@@ -45,18 +44,14 @@ def parasite_axes_class_factory(axes_class=None):
4544
if axes_class is None:
4645
axes_class = Axes
4746

48-
def _get_base_axes_attr(self, attrname):
49-
return getattr(axes_class, attrname)
50-
5147
return type("%sParasite" % axes_class.__name__,
52-
(ParasiteAxesBase, axes_class),
53-
{'_get_base_axes_attr': _get_base_axes_attr})
48+
(ParasiteAxesBase, axes_class), {})
5449

5550

5651
ParasiteAxes = parasite_axes_class_factory()
5752

5853

59-
class ParasiteAxesAuxTransBase(object):
54+
class ParasiteAxesAuxTransBase:
6055
def __init__(self, parent_axes, aux_transform, viewlim_mode=None,
6156
**kwargs):
6257

@@ -80,14 +75,13 @@ def _set_lim_and_transforms(self):
8075

8176
def set_viewlim_mode(self, mode):
8277
if mode not in [None, "equal", "transform"]:
83-
raise ValueError("Unknown mode : %s" % (mode,))
78+
raise ValueError("Unknown mode: %s" % (mode,))
8479
else:
8580
self._viewlim_mode = mode
8681

8782
def get_viewlim_mode(self):
8883
return self._viewlim_mode
8984

90-
9185
def update_viewlim(self):
9286
viewlim = self._parent_axes.viewLim.frozen()
9387
mode = self.get_viewlim_mode()
@@ -96,86 +90,80 @@ def update_viewlim(self):
9690
elif mode == "equal":
9791
self.axes.viewLim.set(viewlim)
9892
elif mode == "transform":
99-
self.axes.viewLim.set(viewlim.transformed(self.transAux.inverted()))
93+
self.axes.viewLim.set(
94+
viewlim.transformed(self.transAux.inverted()))
10095
else:
101-
raise ValueError("Unknown mode : %s" % (self._viewlim_mode,))
102-
96+
raise ValueError("Unknown mode: %s" % (self._viewlim_mode,))
10397

104-
def _pcolor(self, method_name, *XYC, **kwargs):
98+
def _pcolor(self, super_pcolor, *XYC, **kwargs):
10599
if len(XYC) == 1:
106100
C = XYC[0]
107101
ny, nx = C.shape
108102

109-
gx = np.arange(-0.5, nx, 1.)
110-
gy = np.arange(-0.5, ny, 1.)
103+
gx = np.arange(-0.5, nx)
104+
gy = np.arange(-0.5, ny)
111105

112106
X, Y = np.meshgrid(gx, gy)
113107
else:
114108
X, Y, C = XYC
115109

116-
pcolor_routine = self._get_base_axes_attr(method_name)
117-
118110
if "transform" in kwargs:
119-
mesh = pcolor_routine(self, X, Y, C, **kwargs)
111+
mesh = super_pcolor(self, X, Y, C, **kwargs)
120112
else:
121113
orig_shape = X.shape
122-
xy = np.vstack([X.flat, Y.flat])
123-
xyt=xy.transpose()
114+
xyt = np.column_stack([X.flat, Y.flat])
124115
wxy = self.transAux.transform(xyt)
125-
gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape)
126-
mesh = pcolor_routine(self, gx, gy, C, **kwargs)
116+
gx = wxy[:, 0].reshape(orig_shape)
117+
gy = wxy[:, 1].reshape(orig_shape)
118+
mesh = super_pcolor(self, gx, gy, C, **kwargs)
127119
mesh.set_transform(self._parent_axes.transData)
128120

129121
return mesh
130122

131123
def pcolormesh(self, *XYC, **kwargs):
132-
return self._pcolor("pcolormesh", *XYC, **kwargs)
124+
return self._pcolor(super().pcolormesh, *XYC, **kwargs)
133125

134126
def pcolor(self, *XYC, **kwargs):
135-
return self._pcolor("pcolor", *XYC, **kwargs)
136-
127+
return self._pcolor(super().pcolor, *XYC, **kwargs)
137128

138-
def _contour(self, method_name, *XYCL, **kwargs):
129+
def _contour(self, super_contour, *XYCL, **kwargs):
139130

140131
if len(XYCL) <= 2:
141132
C = XYCL[0]
142133
ny, nx = C.shape
143134

144-
gx = np.arange(0., nx, 1.)
145-
gy = np.arange(0., ny, 1.)
135+
gx = np.arange(0., nx)
136+
gy = np.arange(0., ny)
146137

147-
X,Y = np.meshgrid(gx, gy)
138+
X, Y = np.meshgrid(gx, gy)
148139
CL = XYCL
149140
else:
150141
X, Y = XYCL[:2]
151142
CL = XYCL[2:]
152143

153-
contour_routine = self._get_base_axes_attr(method_name)
154-
155144
if "transform" in kwargs:
156-
cont = contour_routine(self, X, Y, *CL, **kwargs)
145+
cont = super_contour(self, X, Y, *CL, **kwargs)
157146
else:
158147
orig_shape = X.shape
159-
xy = np.vstack([X.flat, Y.flat])
160-
xyt=xy.transpose()
148+
xyt = np.column_stack([X.flat, Y.flat])
161149
wxy = self.transAux.transform(xyt)
162-
gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape)
163-
cont = contour_routine(self, gx, gy, *CL, **kwargs)
150+
gx = wxy[:, 0].reshape(orig_shape)
151+
gy = wxy[:, 1].reshape(orig_shape)
152+
cont = super_contour(self, gx, gy, *CL, **kwargs)
164153
for c in cont.collections:
165154
c.set_transform(self._parent_axes.transData)
166155

167156
return cont
168157

169158
def contour(self, *XYCL, **kwargs):
170-
return self._contour("contour", *XYCL, **kwargs)
159+
return self._contour(super().contour, *XYCL, **kwargs)
171160

172161
def contourf(self, *XYCL, **kwargs):
173-
return self._contour("contourf", *XYCL, **kwargs)
162+
return self._contour(super().contourf, *XYCL, **kwargs)
174163

175164
def apply_aspect(self, position=None):
176165
self.update_viewlim()
177-
self._get_base_axes_attr("apply_aspect")(self)
178-
#ParasiteAxes.apply_aspect()
166+
super().apply_aspect()
179167

180168

181169
@functools.lru_cache(None)
@@ -196,23 +184,10 @@ def parasite_axes_auxtrans_class_factory(axes_class=None):
196184
axes_class=ParasiteAxes)
197185

198186

199-
def _get_handles(ax):
200-
handles = ax.lines[:]
201-
handles.extend(ax.patches)
202-
handles.extend([c for c in ax.collections
203-
if isinstance(c, mcoll.LineCollection)])
204-
handles.extend([c for c in ax.collections
205-
if isinstance(c, mcoll.RegularPolyCollection)])
206-
handles.extend([c for c in ax.collections
207-
if isinstance(c, mcoll.CircleCollection)])
208-
209-
return handles
210-
211-
212-
class HostAxesBase(object):
187+
class HostAxesBase:
213188
def __init__(self, *args, **kwargs):
214189
self.parasites = []
215-
self._get_base_axes_attr("__init__")(self, *args, **kwargs)
190+
super().__init__(*args, **kwargs)
216191

217192
def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=None):
218193
parasite_axes_class = parasite_axes_auxtrans_class_factory(axes_class)
@@ -224,13 +199,9 @@ def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=None):
224199
return ax2
225200

226201
def _get_legend_handles(self, legend_handler_map=None):
227-
# don't use this!
228-
Axes_get_legend_handles = self._get_base_axes_attr("_get_legend_handles")
229-
all_handles = list(Axes_get_legend_handles(self, legend_handler_map))
230-
202+
all_handles = super()._get_legend_handles()
231203
for ax in self.parasites:
232204
all_handles.extend(ax._get_legend_handles(legend_handler_map))
233-
234205
return all_handles
235206

236207
def draw(self, renderer):
@@ -257,14 +228,14 @@ def draw(self, renderer):
257228
self.images.extend(images)
258229
self.artists.extend(artists)
259230

260-
self._get_base_axes_attr("draw")(self, renderer)
231+
super().draw(renderer)
261232
self.artists = orig_artists
262233
self.images = orig_images
263234

264235
def cla(self):
265236
for ax in self.parasites:
266237
ax.cla()
267-
self._get_base_axes_attr("cla")(self)
238+
super().cla()
268239

269240
def twinx(self, axes_class=None):
270241
"""
@@ -361,15 +332,10 @@ def _remove_method(h):
361332
return ax2
362333

363334
def get_tightbbox(self, renderer, call_axes_locator=True):
364-
365335
bbs = [ax.get_tightbbox(renderer, call_axes_locator)
366336
for ax in self.parasites]
367-
get_tightbbox = self._get_base_axes_attr("get_tightbbox")
368-
bbs.append(get_tightbbox(self, renderer, call_axes_locator))
369-
370-
_bbox = Bbox.union([b for b in bbs if b.width!=0 or b.height!=0])
371-
372-
return _bbox
337+
bbs.append(super().get_tightbbox(renderer, call_axes_locator))
338+
return Bbox.union([b for b in bbs if b.width != 0 or b.height != 0])
373339

374340

375341
@functools.lru_cache(None)
@@ -380,13 +346,9 @@ def host_axes_class_factory(axes_class=None):
380346
def _get_base_axes(self):
381347
return axes_class
382348

383-
def _get_base_axes_attr(self, attrname):
384-
return getattr(axes_class, attrname)
385-
386349
return type("%sHostAxes" % axes_class.__name__,
387350
(HostAxesBase, axes_class),
388-
{'_get_base_axes_attr': _get_base_axes_attr,
389-
'_get_base_axes': _get_base_axes})
351+
{'_get_base_axes': _get_base_axes})
390352

391353

392354
def host_subplot_class_factory(axes_class):
@@ -421,6 +383,7 @@ def host_axes(*args, axes_class=None, figure=None, **kwargs):
421383
plt.draw_if_interactive()
422384
return ax
423385

386+
424387
def host_subplot(*args, axes_class=None, figure=None, **kwargs):
425388
"""
426389
Create a subplot that can act as a host to parasitic axes.

0 commit comments

Comments
 (0)