Skip to content

Commit c5fb275

Browse files
committed
Fix ngrids support in axes_grid.Grid().
Thiis also restores axes_row and axes_column to their behavior prior to 88e0d84 (which is the commit that broke ngrids support).
1 parent 42472ef commit c5fb275

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/mpl_toolkits/axes_grid1/axes_grid.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ def __init__(self, fig,
144144
axes_array[row, col] = axes_class(
145145
fig, rect, sharex=sharex, sharey=sharey)
146146
self.axes_all = axes_array.ravel(
147-
order="C" if self._direction == "row" else "F").tolist()
148-
self.axes_column = axes_array.T.tolist()
149-
self.axes_row = axes_array.tolist()
147+
order="C" if self._direction == "row" else "F").tolist()[:ngrids]
148+
self.axes_row = [[ax for ax in row if ax] for row in axes_array]
149+
self.axes_column = [[ax for ax in col if ax] for col in axes_array.T]
150150
self.axes_llc = self.axes_column[0][-1]
151151

152152
self._init_locators()
@@ -256,7 +256,10 @@ def set_label_mode(self, mode):
256256
return
257257
for i in range(self._nrows):
258258
for j in range(self._ncols):
259-
ax = self.axes_row[i][j]
259+
try:
260+
ax = self.axes_row[i][j]
261+
except IndexError:
262+
continue
260263
if isinstance(ax.axis, MethodType):
261264
bottom_axis = SimpleAxisArtist(ax.xaxis, 1, ax.spines["bottom"])
262265
left_axis = SimpleAxisArtist(ax.yaxis, 1, ax.spines["left"])

lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,3 +792,9 @@ def test_anchored_locator_base_call():
792792
def test_grid_with_axes_class_not_overriding_axis():
793793
Grid(plt.figure(), 111, (2, 2), axes_class=mpl.axes.Axes)
794794
RGBAxes(plt.figure(), 111, axes_class=mpl.axes.Axes)
795+
796+
797+
def test_grid_ngrids():
798+
fig = plt.figure()
799+
grid = Grid(fig, 111, (3, 3), ngrids=5)
800+
assert len(fig.axes) == grid.ngrids == 5

0 commit comments

Comments
 (0)