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

Skip to content

Commit 979ade2

Browse files
committed
Reuse Grid.__init__ in ImageGrid.__init__.
`ImageGrid.__init__` differs from `Grid.__init__` by some defaults which can be passed to the superclass, and by additionally constructing the colorbar axes. This needs to be pushed into `_update_locators` which isn't optimal but still better than duplicating the entire `Grid.__init__`; the method was thus renamed to `_init_locators` (it's only called during init).
1 parent 3d3c930 commit 979ade2

File tree

1 file changed

+27
-67
lines changed

1 file changed

+27
-67
lines changed

lib/mpl_toolkits/axes_grid1/axes_grid.py

Lines changed: 27 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ def __init__(self, fig,
107107
share_all=False,
108108
share_x=True,
109109
share_y=True,
110-
#aspect=True,
111110
label_mode="L",
112111
axes_class=None,
112+
*,
113+
aspect=False,
113114
):
114115
"""
115116
Parameters
@@ -136,6 +137,7 @@ def __init__(self, fig,
136137
- "all": all axes are labelled.
137138
138139
axes_class : subclass of `matplotlib.axes.Axes`, default: None
140+
aspect : bool, default: False
139141
"""
140142
self._nrows, self._ncols = nrows_ncols
141143

@@ -155,7 +157,7 @@ def __init__(self, fig,
155157
if axes_class is None:
156158
axes_class = self._defaultAxesClass
157159

158-
kw = dict(horizontal=[], vertical=[], aspect=False)
160+
kw = dict(horizontal=[], vertical=[], aspect=aspect)
159161
if isinstance(rect, (str, Number, SubplotSpec)):
160162
self._divider = SubplotDivider(fig, rect, **kw)
161163
elif len(rect) == 3:
@@ -182,7 +184,7 @@ def __init__(self, fig,
182184
self.axes_row = axes_array.tolist()
183185
self.axes_llc = self.axes_column[0][-1]
184186

185-
self._update_locators()
187+
self._init_locators()
186188

187189
if add_all:
188190
for ax in self.axes_all:
@@ -195,7 +197,7 @@ def _init_axes_pad(self, axes_pad):
195197
self._horiz_pad_size = Size.Fixed(axes_pad[0])
196198
self._vert_pad_size = Size.Fixed(axes_pad[1])
197199

198-
def _update_locators(self):
200+
def _init_locators(self):
199201

200202
h = []
201203
h_ax_pos = []
@@ -401,73 +403,20 @@ def __init__(self, fig,
401403
to associated *cbar_axes*.
402404
axes_class : subclass of `matplotlib.axes.Axes`, default: None
403405
"""
404-
self._nrows, self._ncols = nrows_ncols
405-
406-
if ngrids is None:
407-
ngrids = self._nrows * self._ncols
408-
else:
409-
if not 0 < ngrids <= self._nrows * self._ncols:
410-
raise Exception
411-
412-
self.ngrids = ngrids
413-
414-
self._init_axes_pad(axes_pad)
415-
416406
self._colorbar_mode = cbar_mode
417407
self._colorbar_location = cbar_location
418-
if cbar_pad is None:
419-
# horizontal or vertical arrangement?
420-
if cbar_location in ("left", "right"):
421-
self._colorbar_pad = self._horiz_pad_size.fixed_size
422-
else:
423-
self._colorbar_pad = self._vert_pad_size.fixed_size
424-
else:
425-
self._colorbar_pad = cbar_pad
426-
408+
self._colorbar_pad = cbar_pad
427409
self._colorbar_size = cbar_size
410+
# The colorbar axes are created in _init_locators().
428411

429-
cbook._check_in_list(["column", "row"], direction=direction)
430-
self._direction = direction
431-
432-
if axes_class is None:
433-
axes_class = self._defaultAxesClass
434-
435-
kw = dict(horizontal=[], vertical=[], aspect=aspect)
436-
if isinstance(rect, (str, Number, SubplotSpec)):
437-
self._divider = SubplotDivider(fig, rect, **kw)
438-
elif len(rect) == 3:
439-
self._divider = SubplotDivider(fig, *rect, **kw)
440-
elif len(rect) == 4:
441-
self._divider = Divider(fig, rect, **kw)
442-
else:
443-
raise Exception("")
444-
445-
rect = self._divider.get_position()
446-
447-
axes_array = np.full((self._nrows, self._ncols), None, dtype=object)
448-
for i in range(self.ngrids):
449-
col, row = self._get_col_row(i)
450-
if share_all:
451-
sharex = sharey = axes_array[0, 0]
452-
else:
453-
sharex = axes_array[0, col]
454-
sharey = axes_array[row, 0]
455-
axes_array[row, col] = axes_class(
456-
fig, rect, sharex=sharex, sharey=sharey)
457-
self.axes_all = axes_array.ravel().tolist()
458-
self.axes_column = axes_array.T.tolist()
459-
self.axes_row = axes_array.tolist()
460-
self.axes_llc = self.axes_column[0][-1]
461-
462-
self.cbar_axes = [
463-
self._defaultCbarAxesClass(fig, rect,
464-
orientation=self._colorbar_location)
465-
for _ in range(self.ngrids)]
466-
467-
self._update_locators()
412+
super().__init__(
413+
fig, rect, nrows_ncols, ngrids,
414+
direction=direction, axes_pad=axes_pad, add_all=add_all,
415+
share_all=share_all, share_x=True, share_y=True, aspect=aspect,
416+
label_mode=label_mode, axes_class=axes_class)
468417

469418
if add_all:
470-
for ax in self.axes_all+self.cbar_axes:
419+
for ax in self.cbar_axes:
471420
fig.add_axes(ax)
472421

473422
if cbar_set_cax:
@@ -485,9 +434,20 @@ def __init__(self, fig,
485434
for ax, cax in zip(self.axes_all, self.cbar_axes):
486435
ax.cax = cax
487436

488-
self.set_label_mode(label_mode)
437+
def _init_locators(self):
438+
# Slightly abusing this method to inject colorbar creation into init.
489439

490-
def _update_locators(self):
440+
if self._colorbar_pad is None:
441+
# horizontal or vertical arrangement?
442+
if self._colorbar_location in ("left", "right"):
443+
self._colorbar_pad = self._horiz_pad_size.fixed_size
444+
else:
445+
self._colorbar_pad = self._vert_pad_size.fixed_size
446+
self.cbar_axes = [
447+
self._defaultCbarAxesClass(
448+
self.axes_all[0].figure, self._divider.get_position(),
449+
orientation=self._colorbar_location)
450+
for _ in range(self.ngrids)]
491451

492452
cb_mode = self._colorbar_mode
493453
cb_location = self._colorbar_location

0 commit comments

Comments
 (0)