@@ -51,7 +51,7 @@ class Grid:
51
51
in the usage pattern ``grid.axes_row[row][col]``.
52
52
axes_llc : Axes
53
53
The Axes in the lower left corner.
54
- ngrids : int
54
+ naxes : int
55
55
Number of Axes in the grid.
56
56
"""
57
57
@@ -60,7 +60,7 @@ class Grid:
60
60
def __init__ (self , fig ,
61
61
rect ,
62
62
nrows_ncols ,
63
- ngrids = None ,
63
+ naxes = None ,
64
64
direction = "row" ,
65
65
axes_pad = 0.02 ,
66
66
* ,
@@ -83,8 +83,8 @@ def __init__(self, fig,
83
83
``121``), or as a `~.SubplotSpec`.
84
84
nrows_ncols : (int, int)
85
85
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.
88
88
direction : {"row", "column"}, default: "row"
89
89
Whether axes are created in row-major ("row by row") or
90
90
column-major order ("column by column"). This also affects the
@@ -116,14 +116,14 @@ def __init__(self, fig,
116
116
"""
117
117
self ._nrows , self ._ncols = nrows_ncols
118
118
119
- if ngrids is None :
120
- ngrids = self ._nrows * self ._ncols
119
+ if naxes is None :
120
+ naxes = self ._nrows * self ._ncols
121
121
else :
122
- if not 0 < ngrids <= self ._nrows * self ._ncols :
122
+ if not 0 < naxes <= self ._nrows * self ._ncols :
123
123
raise ValueError (
124
- "ngrids must be positive and not larger than nrows*ncols" )
124
+ "naxes must be positive and not larger than nrows*ncols" )
125
125
126
- self .ngrids = ngrids
126
+ self ._naxes = naxes # Store as naxes )
127
127
128
128
self ._horiz_pad_size , self ._vert_pad_size = map (
129
129
Size .Fixed , np .broadcast_to (axes_pad , 2 ))
@@ -150,7 +150,7 @@ def __init__(self, fig,
150
150
rect = self ._divider .get_position ()
151
151
152
152
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 ):
154
154
col , row = self ._get_col_row (i )
155
155
if share_all :
156
156
sharex = sharey = axes_array [0 , 0 ]
@@ -160,9 +160,9 @@ def __init__(self, fig,
160
160
axes_array [row , col ] = axes_class (
161
161
fig , rect , sharex = sharex , sharey = sharey )
162
162
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 ]
166
166
self .axes_llc = self .axes_column [0 ][- 1 ]
167
167
168
168
self ._init_locators ()
@@ -177,7 +177,7 @@ def _init_locators(self):
177
177
[Size .Scaled (1 ), self ._horiz_pad_size ] * (self ._ncols - 1 ) + [Size .Scaled (1 )])
178
178
self ._divider .set_vertical (
179
179
[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 ):
181
181
col , row = self ._get_col_row (i )
182
182
self .axes_all [i ].set_axes_locator (
183
183
self ._divider .new_locator (nx = 2 * col , ny = 2 * (self ._nrows - 1 - row )))
@@ -264,7 +264,10 @@ def set_label_mode(self, mode):
264
264
return
265
265
for i in range (self ._nrows ):
266
266
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
268
271
if isinstance (ax .axis , MethodType ):
269
272
bottom_axis = SimpleAxisArtist (ax .xaxis , 1 , ax .spines ["bottom" ])
270
273
left_axis = SimpleAxisArtist (ax .yaxis , 1 , ax .spines ["left" ])
@@ -297,7 +300,7 @@ class ImageGrid(Grid):
297
300
def __init__ (self , fig ,
298
301
rect ,
299
302
nrows_ncols ,
300
- ngrids = None ,
303
+ naxes = None ,
301
304
direction = "row" ,
302
305
axes_pad = 0.02 ,
303
306
* ,
@@ -321,8 +324,8 @@ def __init__(self, fig,
321
324
as a three-digit subplot position code (e.g., "121").
322
325
nrows_ncols : (int, int)
323
326
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.
326
329
direction : {"row", "column"}, default: "row"
327
330
Whether axes are created in row-major ("row by row") or
328
331
column-major order ("column by column"). This also affects the
@@ -376,7 +379,7 @@ def __init__(self, fig,
376
379
# The colorbar axes are created in _init_locators().
377
380
378
381
super ().__init__ (
379
- fig , rect , nrows_ncols , ngrids ,
382
+ fig , rect , nrows_ncols , naxes ,
380
383
direction = direction , axes_pad = axes_pad ,
381
384
share_all = share_all , share_x = True , share_y = True , aspect = aspect ,
382
385
label_mode = label_mode , axes_class = axes_class )
@@ -412,7 +415,7 @@ def _init_locators(self):
412
415
_cbaraxes_class_factory (self ._defaultAxesClass )(
413
416
self .axes_all [0 ].get_figure (root = False ), self ._divider .get_position (),
414
417
orientation = self ._colorbar_location )
415
- for _ in range (self .ngrids )]
418
+ for _ in range (self .naxes )]
416
419
417
420
cb_mode = self ._colorbar_mode
418
421
cb_location = self ._colorbar_location
@@ -433,7 +436,7 @@ def _init_locators(self):
433
436
v .append (Size .from_any (self ._colorbar_size , sz ))
434
437
v .append (Size .from_any (self ._colorbar_pad , sz ))
435
438
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 ):
437
440
self .cbar_axes [i ].set_visible (False )
438
441
self .cbar_axes [0 ].set_axes_locator (locator )
439
442
self .cbar_axes [0 ].set_visible (True )
@@ -494,7 +497,7 @@ def _init_locators(self):
494
497
v_cb_pos .append (len (v ))
495
498
v .append (Size .from_any (self ._colorbar_size , sz ))
496
499
497
- for i in range (self .ngrids ):
500
+ for i in range (self .naxes ):
498
501
col , row = self ._get_col_row (i )
499
502
locator = self ._divider .new_locator (nx = h_ax_pos [col ],
500
503
ny = v_ax_pos [self ._nrows - 1 - row ])
@@ -534,12 +537,12 @@ def _init_locators(self):
534
537
v .append (Size .from_any (self ._colorbar_size , sz ))
535
538
locator = self ._divider .new_locator (nx = 0 , nx1 = - 1 , ny = - 2 )
536
539
if cb_location in ("right" , "top" ):
537
- for i in range (self .ngrids ):
540
+ for i in range (self .naxes ):
538
541
self .cbar_axes [i ].set_visible (False )
539
542
self .cbar_axes [0 ].set_axes_locator (locator )
540
543
self .cbar_axes [0 ].set_visible (True )
541
544
elif cb_mode == "each" :
542
- for i in range (self .ngrids ):
545
+ for i in range (self .naxes ):
543
546
self .cbar_axes [i ].set_visible (True )
544
547
elif cb_mode == "edge" :
545
548
if cb_location in ("right" , "left" ):
@@ -548,10 +551,10 @@ def _init_locators(self):
548
551
count = self ._ncols
549
552
for i in range (count ):
550
553
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 ):
552
555
self .cbar_axes [j ].set_visible (False )
553
556
else :
554
- for i in range (self .ngrids ):
557
+ for i in range (self .naxes ):
555
558
self .cbar_axes [i ].set_visible (False )
556
559
self .cbar_axes [i ].set_position ([1. , 1. , 0.001 , 0.001 ],
557
560
which = "active" )
0 commit comments