@@ -51,7 +51,7 @@ class Grid:
5151 in the usage pattern ``grid.axes_row[row][col]``.
5252 axes_llc : Axes
5353 The Axes in the lower left corner.
54- ngrids : int
54+ naxes : int
5555 Number of Axes in the grid.
5656 """
5757
@@ -60,7 +60,7 @@ class Grid:
6060 def __init__ (self , fig ,
6161 rect ,
6262 nrows_ncols ,
63- ngrids = None ,
63+ naxes = None ,
6464 direction = "row" ,
6565 axes_pad = 0.02 ,
6666 * ,
@@ -83,8 +83,8 @@ def __init__(self, fig,
8383 ``121``), or as a `~.SubplotSpec`.
8484 nrows_ncols : (int, int)
8585 Number of rows and columns in the grid.
86- ngrids : int or None, default: None
87- If not None, only the first *ngrids * axes in the grid are created.
86+ naxes : int or None, default: None
87+ If not None, only the first *naxes * axes in the grid are created.
8888 direction : {"row", "column"}, default: "row"
8989 Whether axes are created in row-major ("row by row") or
9090 column-major order ("column by column"). This also affects the
@@ -116,14 +116,14 @@ def __init__(self, fig,
116116 """
117117 self ._nrows , self ._ncols = nrows_ncols
118118
119- if ngrids is None :
120- ngrids = self ._nrows * self ._ncols
119+ if naxes is None :
120+ naxes = self ._nrows * self ._ncols
121121 else :
122- if not 0 < ngrids <= self ._nrows * self ._ncols :
122+ if not 0 < naxes <= self ._nrows * self ._ncols :
123123 raise ValueError (
124- "ngrids must be positive and not larger than nrows*ncols" )
124+ "naxes must be positive and not larger than nrows*ncols" )
125125
126- self .ngrids = ngrids
126+ self ._naxes = naxes # Store as naxes )
127127
128128 self ._horiz_pad_size , self ._vert_pad_size = map (
129129 Size .Fixed , np .broadcast_to (axes_pad , 2 ))
@@ -150,7 +150,7 @@ def __init__(self, fig,
150150 rect = self ._divider .get_position ()
151151
152152 axes_array = np .full ((self ._nrows , self ._ncols ), None , dtype = object )
153- for i in range (self .ngrids ):
153+ for i in range (self .naxes ):
154154 col , row = self ._get_col_row (i )
155155 if share_all :
156156 sharex = sharey = axes_array [0 , 0 ]
@@ -160,9 +160,9 @@ def __init__(self, fig,
160160 axes_array [row , col ] = axes_class (
161161 fig , rect , sharex = sharex , sharey = sharey )
162162 self .axes_all = axes_array .ravel (
163- order = "C" if self ._direction == "row" else "F" ).tolist ()
164- self .axes_column = axes_array . T . tolist ()
165- self .axes_row = axes_array .tolist ()
163+ order = "C" if self ._direction == "row" else "F" ).tolist ()[: naxes ]
164+ self .axes_row = [[ ax for ax in row if ax ] for row in axes_array ]
165+ self .axes_column = [[ ax for ax in col if ax ] for col in axes_array .T ]
166166 self .axes_llc = self .axes_column [0 ][- 1 ]
167167
168168 self ._init_locators ()
@@ -177,7 +177,7 @@ def _init_locators(self):
177177 [Size .Scaled (1 ), self ._horiz_pad_size ] * (self ._ncols - 1 ) + [Size .Scaled (1 )])
178178 self ._divider .set_vertical (
179179 [Size .Scaled (1 ), self ._vert_pad_size ] * (self ._nrows - 1 ) + [Size .Scaled (1 )])
180- for i in range (self .ngrids ):
180+ for i in range (self .naxes ):
181181 col , row = self ._get_col_row (i )
182182 self .axes_all [i ].set_axes_locator (
183183 self ._divider .new_locator (nx = 2 * col , ny = 2 * (self ._nrows - 1 - row )))
@@ -264,7 +264,10 @@ def set_label_mode(self, mode):
264264 return
265265 for i in range (self ._nrows ):
266266 for j in range (self ._ncols ):
267- ax = self .axes_row [i ][j ]
267+ try :
268+ ax = self .axes_row [i ][j ]
269+ except IndexError :
270+ continue
268271 if isinstance (ax .axis , MethodType ):
269272 bottom_axis = SimpleAxisArtist (ax .xaxis , 1 , ax .spines ["bottom" ])
270273 left_axis = SimpleAxisArtist (ax .yaxis , 1 , ax .spines ["left" ])
@@ -297,7 +300,7 @@ class ImageGrid(Grid):
297300 def __init__ (self , fig ,
298301 rect ,
299302 nrows_ncols ,
300- ngrids = None ,
303+ naxes = None ,
301304 direction = "row" ,
302305 axes_pad = 0.02 ,
303306 * ,
@@ -321,8 +324,8 @@ def __init__(self, fig,
321324 as a three-digit subplot position code (e.g., "121").
322325 nrows_ncols : (int, int)
323326 Number of rows and columns in the grid.
324- ngrids : int or None, default: None
325- If not None, only the first *ngrids * axes in the grid are created.
327+ naxes : int or None, default: None
328+ If not None, only the first *naxes * axes in the grid are created.
326329 direction : {"row", "column"}, default: "row"
327330 Whether axes are created in row-major ("row by row") or
328331 column-major order ("column by column"). This also affects the
@@ -376,7 +379,7 @@ def __init__(self, fig,
376379 # The colorbar axes are created in _init_locators().
377380
378381 super ().__init__ (
379- fig , rect , nrows_ncols , ngrids ,
382+ fig , rect , nrows_ncols , naxes ,
380383 direction = direction , axes_pad = axes_pad ,
381384 share_all = share_all , share_x = True , share_y = True , aspect = aspect ,
382385 label_mode = label_mode , axes_class = axes_class )
@@ -412,7 +415,7 @@ def _init_locators(self):
412415 _cbaraxes_class_factory (self ._defaultAxesClass )(
413416 self .axes_all [0 ].get_figure (root = False ), self ._divider .get_position (),
414417 orientation = self ._colorbar_location )
415- for _ in range (self .ngrids )]
418+ for _ in range (self .naxes )]
416419
417420 cb_mode = self ._colorbar_mode
418421 cb_location = self ._colorbar_location
@@ -433,7 +436,7 @@ def _init_locators(self):
433436 v .append (Size .from_any (self ._colorbar_size , sz ))
434437 v .append (Size .from_any (self ._colorbar_pad , sz ))
435438 locator = self ._divider .new_locator (nx = 0 , nx1 = - 1 , ny = 0 )
436- for i in range (self .ngrids ):
439+ for i in range (self .naxes ):
437440 self .cbar_axes [i ].set_visible (False )
438441 self .cbar_axes [0 ].set_axes_locator (locator )
439442 self .cbar_axes [0 ].set_visible (True )
@@ -494,7 +497,7 @@ def _init_locators(self):
494497 v_cb_pos .append (len (v ))
495498 v .append (Size .from_any (self ._colorbar_size , sz ))
496499
497- for i in range (self .ngrids ):
500+ for i in range (self .naxes ):
498501 col , row = self ._get_col_row (i )
499502 locator = self ._divider .new_locator (nx = h_ax_pos [col ],
500503 ny = v_ax_pos [self ._nrows - 1 - row ])
@@ -534,12 +537,12 @@ def _init_locators(self):
534537 v .append (Size .from_any (self ._colorbar_size , sz ))
535538 locator = self ._divider .new_locator (nx = 0 , nx1 = - 1 , ny = - 2 )
536539 if cb_location in ("right" , "top" ):
537- for i in range (self .ngrids ):
540+ for i in range (self .naxes ):
538541 self .cbar_axes [i ].set_visible (False )
539542 self .cbar_axes [0 ].set_axes_locator (locator )
540543 self .cbar_axes [0 ].set_visible (True )
541544 elif cb_mode == "each" :
542- for i in range (self .ngrids ):
545+ for i in range (self .naxes ):
543546 self .cbar_axes [i ].set_visible (True )
544547 elif cb_mode == "edge" :
545548 if cb_location in ("right" , "left" ):
@@ -548,10 +551,10 @@ def _init_locators(self):
548551 count = self ._ncols
549552 for i in range (count ):
550553 self .cbar_axes [i ].set_visible (True )
551- for j in range (i + 1 , self .ngrids ):
554+ for j in range (i + 1 , self .naxes ):
552555 self .cbar_axes [j ].set_visible (False )
553556 else :
554- for i in range (self .ngrids ):
557+ for i in range (self .naxes ):
555558 self .cbar_axes [i ].set_visible (False )
556559 self .cbar_axes [i ].set_position ([1. , 1. , 0.001 , 0.001 ],
557560 which = "active" )
0 commit comments