@@ -57,10 +57,11 @@ class Grid:
57
57
58
58
_defaultAxesClass = Axes
59
59
60
+ @_api .rename_parameter ("3.11" ,"ngrids" ,"n_axes" )
60
61
def __init__ (self ,fig ,
61
62
rect ,
62
63
nrows_ncols ,
63
- ngrids = None ,
64
+ n_axes = None ,
64
65
direction = "row" ,
65
66
axes_pad = 0.02 ,
66
67
* ,
@@ -83,8 +84,8 @@ def __init__(self, fig,
83
84
``121``), or as a `~.SubplotSpec`.
84
85
nrows_ncols : (int, int)
85
86
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.
87
+ n_axes : int or None, default: None
88
+ If not None, only the first *n_axes * axes in the grid are created.
88
89
direction : {"row", "column"}, default: "row"
89
90
Whether axes are created in row-major ("row by row") or
90
91
column-major order ("column by column"). This also affects the
@@ -116,14 +117,14 @@ def __init__(self, fig,
116
117
"""
117
118
self ._nrows ,self ._ncols = nrows_ncols
118
119
119
- if ngrids is None :
120
- ngrids = self ._nrows * self ._ncols
120
+ if n_axes is None :
121
+ n_axes = self ._nrows * self ._ncols
121
122
else :
122
- if not 0 < ngrids <= self ._nrows * self ._ncols :
123
+ if not 0 < n_axes <= self ._nrows * self ._ncols :
123
124
raise ValueError (
124
- "ngrids must be positive and not larger than nrows*ncols" )
125
+ "n_axes must be positive and not larger than nrows*ncols" )
125
126
126
- self .ngrids = ngrids
127
+ self .n_axes = n_axes
127
128
128
129
self ._horiz_pad_size ,self ._vert_pad_size = map (
129
130
Size .Fixed ,np .broadcast_to (axes_pad ,2 ))
@@ -150,7 +151,7 @@ def __init__(self, fig,
150
151
rect = self ._divider .get_position ()
151
152
152
153
axes_array = np .full ((self ._nrows ,self ._ncols ),None ,dtype = object )
153
- for i in range (self .ngrids ):
154
+ for i in range (self .n_axes ):
154
155
col ,row = self ._get_col_row (i )
155
156
if share_all :
156
157
sharex = sharey = axes_array [0 ,0 ]
@@ -160,9 +161,9 @@ def __init__(self, fig,
160
161
axes_array [row ,col ]= axes_class (
161
162
fig ,rect ,sharex = sharex ,sharey = sharey )
162
163
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 ()
164
+ order = "C" if self ._direction == "row" else "F" ).tolist ()[: n_axes ]
165
+ self .axes_row = [[ ax for ax in row if ax ] for row in axes_array ]
166
+ self .axes_column = [[ ax for ax in col if ax ] for col in axes_array .T ]
166
167
self .axes_llc = self .axes_column [0 ][- 1 ]
167
168
168
169
self ._init_locators ()
@@ -177,7 +178,7 @@ def _init_locators(self):
177
178
[Size .Scaled (1 ),self ._horiz_pad_size ]* (self ._ncols - 1 )+ [Size .Scaled (1 )])
178
179
self ._divider .set_vertical (
179
180
[Size .Scaled (1 ),self ._vert_pad_size ]* (self ._nrows - 1 )+ [Size .Scaled (1 )])
180
- for i in range (self .ngrids ):
181
+ for i in range (self .n_axes ):
181
182
col ,row = self ._get_col_row (i )
182
183
self .axes_all [i ].set_axes_locator (
183
184
self ._divider .new_locator (nx = 2 * col ,ny = 2 * (self ._nrows - 1 - row )))
@@ -190,6 +191,8 @@ def _get_col_row(self, n):
190
191
191
192
return col ,row
192
193
194
+ ngrids = _api .deprecated (property (lambda self :self .n_axes ))
195
+
193
196
# Good to propagate __len__ if we have __getitem__
194
197
def __len__ (self ):
195
198
return len (self .axes_all )
@@ -254,7 +257,10 @@ def set_label_mode(self, mode):
254
257
if mode == "keep" :
255
258
return
256
259
for i ,j in np .ndindex (self ._nrows ,self ._ncols ):
257
- ax = self .axes_row [i ][j ]
260
+ try :
261
+ ax = self .axes_row [i ][j ]
262
+ except IndexError :
263
+ continue
258
264
if isinstance (ax .axis ,MethodType ):
259
265
bottom_axis = SimpleAxisArtist (ax .xaxis ,1 ,ax .spines ["bottom" ])
260
266
left_axis = SimpleAxisArtist (ax .yaxis ,1 ,ax .spines ["left" ])
@@ -293,7 +299,7 @@ class ImageGrid(Grid):
293
299
def __init__ (self ,fig ,
294
300
rect ,
295
301
nrows_ncols ,
296
- ngrids = None ,
302
+ n_axes = None ,
297
303
direction = "row" ,
298
304
axes_pad = 0.02 ,
299
305
* ,
@@ -317,8 +323,8 @@ def __init__(self, fig,
317
323
as a three-digit subplot position code (e.g., "121").
318
324
nrows_ncols : (int, int)
319
325
Number of rows and columns in the grid.
320
- ngrids : int or None, default: None
321
- If not None, only the first *ngrids * axes in the grid are created.
326
+ n_axes : int or None, default: None
327
+ If not None, only the first *n_axes * axes in the grid are created.
322
328
direction : {"row", "column"}, default: "row"
323
329
Whether axes are created in row-major ("row by row") or
324
330
column-major order ("column by column"). This also affects the
@@ -372,7 +378,7 @@ def __init__(self, fig,
372
378
# The colorbar axes are created in _init_locators().
373
379
374
380
super ().__init__ (
375
- fig ,rect ,nrows_ncols ,ngrids ,
381
+ fig ,rect ,nrows_ncols ,n_axes ,
376
382
direction = direction ,axes_pad = axes_pad ,
377
383
share_all = share_all ,share_x = True ,share_y = True ,aspect = aspect ,
378
384
label_mode = label_mode ,axes_class = axes_class )
@@ -408,7 +414,7 @@ def _init_locators(self):
408
414
_cbaraxes_class_factory (self ._defaultAxesClass )(
409
415
self .axes_all [0 ].get_figure (root = False ),self ._divider .get_position (),
410
416
orientation = self ._colorbar_location )
411
- for _ in range (self .ngrids )]
417
+ for _ in range (self .n_axes )]
412
418
413
419
cb_mode = self ._colorbar_mode
414
420
cb_location = self ._colorbar_location
@@ -429,7 +435,7 @@ def _init_locators(self):
429
435
v .append (Size .from_any (self ._colorbar_size ,sz ))
430
436
v .append (Size .from_any (self ._colorbar_pad ,sz ))
431
437
locator = self ._divider .new_locator (nx = 0 ,nx1 = - 1 ,ny = 0 )
432
- for i in range (self .ngrids ):
438
+ for i in range (self .n_axes ):
433
439
self .cbar_axes [i ].set_visible (False )
434
440
self .cbar_axes [0 ].set_axes_locator (locator )
435
441
self .cbar_axes [0 ].set_visible (True )
@@ -490,7 +496,7 @@ def _init_locators(self):
490
496
v_cb_pos .append (len (v ))
491
497
v .append (Size .from_any (self ._colorbar_size ,sz ))
492
498
493
- for i in range (self .ngrids ):
499
+ for i in range (self .n_axes ):
494
500
col ,row = self ._get_col_row (i )
495
501
locator = self ._divider .new_locator (nx = h_ax_pos [col ],
496
502
ny = v_ax_pos [self ._nrows - 1 - row ])
@@ -530,12 +536,12 @@ def _init_locators(self):
530
536
v .append (Size .from_any (self ._colorbar_size ,sz ))
531
537
locator = self ._divider .new_locator (nx = 0 ,nx1 = - 1 ,ny = - 2 )
532
538
if cb_location in ("right" ,"top" ):
533
- for i in range (self .ngrids ):
539
+ for i in range (self .n_axes ):
534
540
self .cbar_axes [i ].set_visible (False )
535
541
self .cbar_axes [0 ].set_axes_locator (locator )
536
542
self .cbar_axes [0 ].set_visible (True )
537
543
elif cb_mode == "each" :
538
- for i in range (self .ngrids ):
544
+ for i in range (self .n_axes ):
539
545
self .cbar_axes [i ].set_visible (True )
540
546
elif cb_mode == "edge" :
541
547
if cb_location in ("right" ,"left" ):
@@ -544,10 +550,10 @@ def _init_locators(self):
544
550
count = self ._ncols
545
551
for i in range (count ):
546
552
self .cbar_axes [i ].set_visible (True )
547
- for j in range (i + 1 ,self .ngrids ):
553
+ for j in range (i + 1 ,self .n_axes ):
548
554
self .cbar_axes [j ].set_visible (False )
549
555
else :
550
- for i in range (self .ngrids ):
556
+ for i in range (self .n_axes ):
551
557
self .cbar_axes [i ].set_visible (False )
552
558
self .cbar_axes [i ].set_position ([1. ,1. ,0.001 ,0.001 ],
553
559
which = "active" )