@@ -384,6 +384,7 @@ def show(self, warn=True):
384
384
AttributeError.
385
385
386
386
.. warning::
387
+
387
388
This does not manage an GUI event loop. Consequently, the figure
388
389
may only be shown briefly or not shown at all if you or your
389
390
environment are not managing an event loop.
@@ -1264,13 +1265,16 @@ def add_subplot(self, *args, **kwargs):
1264
1265
1265
1266
Parameters
1266
1267
----------
1267
- *args, int or (int, int, int) or `SubplotSpec`, default: (1, 1, 1)
1268
+ *args : int, (int, int, *index*), or `. SubplotSpec`, default: (1, 1, 1)
1268
1269
The position of the subplot described by one of
1269
1270
1270
1271
- Three integers (*nrows*, *ncols*, *index*). The subplot will
1271
1272
take the *index* position on a grid with *nrows* rows and
1272
1273
*ncols* columns. *index* starts at 1 in the upper left corner
1273
- and increases to the right.
1274
+ and increases to the right. *index* can also be a two-tuple
1275
+ specifying the (*first*, *last*) indices (1-based, and including
1276
+ *last*) of the subplot, e.g., ``fig.add_subplot(3, 1, (1, 2))``
1277
+ makes a subplot that spans the upper 2/3 of the figure.
1274
1278
- A 3-digit integer. The digits are interpreted as if given
1275
1279
separately as three single-digit integers, i.e.
1276
1280
``fig.add_subplot(235)`` is the same as
@@ -1362,48 +1366,27 @@ def add_subplot(self, *args, **kwargs):
1362
1366
raise TypeError (
1363
1367
"add_subplot() got an unexpected keyword argument 'figure'" )
1364
1368
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 ):
1369
+ if len (args ) == 1 and isinstance (args [0 ], SubplotBase ):
1393
1370
ax = args [0 ]
1394
1371
if ax .get_figure () is not self :
1395
1372
raise ValueError ("The Subplot must have been created in "
1396
1373
"the present figure" )
1397
1374
# make a key for the subplot (which includes the axes object id
1398
1375
# in the hash)
1399
1376
key = self ._make_key (* args , ** kwargs )
1377
+
1400
1378
else :
1379
+ if not args :
1380
+ args = (1 , 1 , 1 )
1381
+ # Normalize correct ijk values to (i, j, k) here so that
1382
+ # add_subplot(111) == add_subplot(1, 1, 1). Invalid values will
1383
+ # trigger errors later (via SubplotSpec._from_subplot_args).
1384
+ if (len (args ) == 1 and isinstance (args [0 ], Integral )
1385
+ and 100 <= args [0 ] <= 999 ):
1386
+ args = tuple (map (int , str (args [0 ])))
1401
1387
projection_class , kwargs , key = \
1402
1388
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
-
1389
+ ax = self ._axstack .get (key ) # search axes with this key in stack
1407
1390
if ax is not None :
1408
1391
if isinstance (ax , projection_class ):
1409
1392
# the axes already existed, so set it as active & return
@@ -1416,7 +1399,6 @@ def add_subplot(self, *args, **kwargs):
1416
1399
# Without this, add_subplot would be simpler and
1417
1400
# more similar to add_axes.
1418
1401
self ._axstack .remove (ax )
1419
-
1420
1402
ax = subplot_class_factory (projection_class )(self , * args , ** kwargs )
1421
1403
1422
1404
return self ._add_axes_internal (key , ax )
0 commit comments