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

Skip to content

Commit cd500c7

Browse files
committed
Use super() instead of manually fetching supermethods for parasite axes.
(This manual fetching was previously done in _get_base_axes_attr.)
1 parent 250c33e commit cd500c7

File tree

1 file changed

+24
-50
lines changed

1 file changed

+24
-50
lines changed

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 24 additions & 50 deletions
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

@@ -87,7 +82,6 @@ def set_viewlim_mode(self, mode):
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()
@@ -100,8 +94,7 @@ def update_viewlim(self):
10094
else:
10195
raise ValueError("Unknown mode : %s" % (self._viewlim_mode,))
10296

103-
104-
def _pcolor(self, method_name, *XYC, **kwargs):
97+
def _pcolor(self, super_pcolor, *XYC, **kwargs):
10598
if len(XYC) == 1:
10699
C = XYC[0]
107100
ny, nx = C.shape
@@ -113,29 +106,26 @@ def _pcolor(self, method_name, *XYC, **kwargs):
113106
else:
114107
X, Y, C = XYC
115108

116-
pcolor_routine = self._get_base_axes_attr(method_name)
117-
118109
if "transform" in kwargs:
119-
mesh = pcolor_routine(self, X, Y, C, **kwargs)
110+
mesh = super_pcolor(self, X, Y, C, **kwargs)
120111
else:
121112
orig_shape = X.shape
122113
xy = np.vstack([X.flat, Y.flat])
123114
xyt=xy.transpose()
124115
wxy = self.transAux.transform(xyt)
125116
gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape)
126-
mesh = pcolor_routine(self, gx, gy, C, **kwargs)
117+
mesh = super_pcolor(self, gx, gy, C, **kwargs)
127118
mesh.set_transform(self._parent_axes.transData)
128119

129120
return mesh
130121

131122
def pcolormesh(self, *XYC, **kwargs):
132-
return self._pcolor("pcolormesh", *XYC, **kwargs)
123+
return self._pcolor(super().pcolormesh, *XYC, **kwargs)
133124

134125
def pcolor(self, *XYC, **kwargs):
135-
return self._pcolor("pcolor", *XYC, **kwargs)
136-
126+
return self._pcolor(super().pcolor, *XYC, **kwargs)
137127

138-
def _contour(self, method_name, *XYCL, **kwargs):
128+
def _contour(self, super_contour, *XYCL, **kwargs):
139129

140130
if len(XYCL) <= 2:
141131
C = XYCL[0]
@@ -150,32 +140,29 @@ def _contour(self, method_name, *XYCL, **kwargs):
150140
X, Y = XYCL[:2]
151141
CL = XYCL[2:]
152142

153-
contour_routine = self._get_base_axes_attr(method_name)
154-
155143
if "transform" in kwargs:
156-
cont = contour_routine(self, X, Y, *CL, **kwargs)
144+
cont = super_contour(self, X, Y, *CL, **kwargs)
157145
else:
158146
orig_shape = X.shape
159147
xy = np.vstack([X.flat, Y.flat])
160148
xyt=xy.transpose()
161149
wxy = self.transAux.transform(xyt)
162150
gx, gy = wxy[:,0].reshape(orig_shape), wxy[:,1].reshape(orig_shape)
163-
cont = contour_routine(self, gx, gy, *CL, **kwargs)
151+
cont = super_contour(self, gx, gy, *CL, **kwargs)
164152
for c in cont.collections:
165153
c.set_transform(self._parent_axes.transData)
166154

167155
return cont
168156

169157
def contour(self, *XYCL, **kwargs):
170-
return self._contour("contour", *XYCL, **kwargs)
158+
return self._contour(super().contour, *XYCL, **kwargs)
171159

172160
def contourf(self, *XYCL, **kwargs):
173-
return self._contour("contourf", *XYCL, **kwargs)
161+
return self._contour(super().contourf, *XYCL, **kwargs)
174162

175163
def apply_aspect(self, position=None):
176164
self.update_viewlim()
177-
self._get_base_axes_attr("apply_aspect")(self)
178-
#ParasiteAxes.apply_aspect()
165+
super().apply_aspect()
179166

180167

181168
@functools.lru_cache(None)
@@ -209,10 +196,10 @@ def _get_handles(ax):
209196
return handles
210197

211198

212-
class HostAxesBase(object):
199+
class HostAxesBase:
213200
def __init__(self, *args, **kwargs):
214201
self.parasites = []
215-
self._get_base_axes_attr("__init__")(self, *args, **kwargs)
202+
super().__init__(*args, **kwargs)
216203

217204
def get_aux_axes(self, tr, viewlim_mode="equal", axes_class=None):
218205
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):
224211
return ax2
225212

226213
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-
214+
all_handles = super()._get_legend_handles()
231215
for ax in self.parasites:
232216
all_handles.extend(ax._get_legend_handles(legend_handler_map))
233-
234217
return all_handles
235218

236219
def draw(self, renderer):
@@ -257,14 +240,14 @@ def draw(self, renderer):
257240
self.images.extend(images)
258241
self.artists.extend(artists)
259242

260-
self._get_base_axes_attr("draw")(self, renderer)
243+
super().draw(renderer)
261244
self.artists = orig_artists
262245
self.images = orig_images
263246

264247
def cla(self):
265248
for ax in self.parasites:
266249
ax.cla()
267-
self._get_base_axes_attr("cla")(self)
250+
super().cla()
268251

269252
def twinx(self, axes_class=None):
270253
"""
@@ -361,15 +344,10 @@ def _remove_method(h):
361344
return ax2
362345

363346
def get_tightbbox(self, renderer, call_axes_locator=True):
364-
365347
bbs = [ax.get_tightbbox(renderer, call_axes_locator)
366348
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
349+
bbs.append(super().get_tightbbox(renderer, call_axes_locator))
350+
return Bbox.union([b for b in bbs if b.width!=0 or b.height!=0])
373351

374352

375353
@functools.lru_cache(None)
@@ -380,13 +358,9 @@ def host_axes_class_factory(axes_class=None):
380358
def _get_base_axes(self):
381359
return axes_class
382360

383-
def _get_base_axes_attr(self, attrname):
384-
return getattr(axes_class, attrname)
385-
386361
return type("%sHostAxes" % axes_class.__name__,
387362
(HostAxesBase, axes_class),
388-
{'_get_base_axes_attr': _get_base_axes_attr,
389-
'_get_base_axes': _get_base_axes})
363+
{'_get_base_axes': _get_base_axes})
390364

391365

392366
def host_subplot_class_factory(axes_class):

0 commit comments

Comments
 (0)