@@ -1264,13 +1264,16 @@ def add_subplot(self, *args, **kwargs):
1264
1264
1265
1265
Parameters
1266
1266
----------
1267
- *args, int or (int, int, int) or `SubplotSpec`, default: (1, 1, 1)
1267
+ *args, int, (int, int, *index*), or `SubplotSpec`, default: (1, 1, 1)
1268
1268
The position of the subplot described by one of
1269
1269
1270
1270
- Three integers (*nrows*, *ncols*, *index*). The subplot will
1271
1271
take the *index* position on a grid with *nrows* rows and
1272
1272
*ncols* columns. *index* starts at 1 in the upper left corner
1273
- and increases to the right.
1273
+ and increases to the right. *index* can also be a two-tuple
1274
+ specifying the (*first*, *last*) indices (1-based, and including
1275
+ *last*) of the subplot, e.g., ``fig.add_subplot(3, 1, (1, 2))``
1276
+ makes a subplot that spans the upper 2/3 of the figure.
1274
1277
- A 3-digit integer. The digits are interpreted as if given
1275
1278
separately as three single-digit integers, i.e.
1276
1279
``fig.add_subplot(235)`` is the same as
@@ -1362,48 +1365,21 @@ def add_subplot(self, *args, **kwargs):
1362
1365
raise TypeError (
1363
1366
"add_subplot() got an unexpected keyword argument 'figure'" )
1364
1367
1365
- nargs = len (args )
1366
- if nargs == 0 :
1367
- args = (1 , 1 , 1 )
1368
- elif nargs == 1 :
1369
- if isinstance (args [0 ], Integral ):
1370
- if not 100 <= args [0 ] <= 999 :
1371
- raise ValueError (f"Integer subplot specification must be "
1372
- f"a three-digit number, not { args [0 ]} " )
1373
- args = tuple (map (int , str (args [0 ])))
1374
- elif isinstance (args [0 ], (SubplotBase , SubplotSpec )):
1375
- pass # no further validation or normalization needed
1376
- else :
1377
- raise TypeError ('Positional arguments are not a valid '
1378
- 'position specification.' )
1379
- elif nargs == 3 :
1380
- for arg in args :
1381
- if not isinstance (arg , Integral ):
1382
- cbook .warn_deprecated (
1383
- "3.3" ,
1384
- message = "Passing non-integers as three-element "
1385
- "position specification is deprecated since "
1386
- "%(since)s and will be removed %(removal)s." )
1387
- args = tuple (map (int , args ))
1388
- else :
1389
- raise TypeError (f'add_subplot() takes 1 or 3 positional arguments '
1390
- f'but { nargs } were given' )
1391
-
1392
- if isinstance (args [0 ], SubplotBase ):
1368
+ if len (args ) == 1 and isinstance (args [0 ], SubplotBase ):
1393
1369
ax = args [0 ]
1394
1370
if ax .get_figure () is not self :
1395
1371
raise ValueError ("The Subplot must have been created in "
1396
1372
"the present figure" )
1397
1373
# make a key for the subplot (which includes the axes object id
1398
1374
# in the hash)
1399
1375
key = self ._make_key (* args , ** kwargs )
1376
+
1400
1377
else :
1378
+ if not args :
1379
+ args = (1 , 1 , 1 )
1401
1380
projection_class , kwargs , key = \
1402
1381
self ._process_projection_requirements (* args , ** kwargs )
1403
-
1404
- # try to find the axes with this key in the stack
1405
- ax = self ._axstack .get (key )
1406
-
1382
+ ax = self ._axstack .get (key ) # search axes with this key in stack
1407
1383
if ax is not None :
1408
1384
if isinstance (ax , projection_class ):
1409
1385
# the axes already existed, so set it as active & return
@@ -1416,7 +1392,6 @@ def add_subplot(self, *args, **kwargs):
1416
1392
# Without this, add_subplot would be simpler and
1417
1393
# more similar to add_axes.
1418
1394
self ._axstack .remove (ax )
1419
-
1420
1395
ax = subplot_class_factory (projection_class )(self , * args , ** kwargs )
1421
1396
1422
1397
return self ._add_axes_internal (key , ax )
0 commit comments