|
27 | 27 |
|
28 | 28 | from matplotlib.axes import Axes, SubplotBase, subplot_class_factory |
29 | 29 | from matplotlib.blocking_input import BlockingMouseInput, BlockingKeyMouseInput |
30 | | -from matplotlib.gridspec import GridSpec |
| 30 | +from matplotlib.gridspec import GridSpec, SubplotSpec |
31 | 31 | import matplotlib.legend as mlegend |
32 | 32 | from matplotlib.patches import Rectangle |
33 | 33 | from matplotlib.text import Text |
@@ -1254,19 +1254,19 @@ def add_subplot(self, *args, **kwargs): |
1254 | 1254 |
|
1255 | 1255 | Parameters |
1256 | 1256 | ---------- |
1257 | | - *args, default: (1, 1, 1) |
1258 | | - Either a 3-digit integer or three separate integers |
1259 | | - describing the position of the subplot. If the three |
1260 | | - integers are *nrows*, *ncols*, and *index* in order, the |
1261 | | - subplot will take the *index* position on a grid with *nrows* |
1262 | | - rows and *ncols* columns. *index* starts at 1 in the upper left |
1263 | | - corner and increases to the right. |
1264 | | -
|
1265 | | - *pos* is a three digit integer, where the first digit is the |
1266 | | - number of rows, the second the number of columns, and the third |
1267 | | - the index of the subplot. i.e. fig.add_subplot(235) is the same as |
1268 | | - fig.add_subplot(2, 3, 5). Note that all integers must be less than |
1269 | | - 10 for this form to work. |
| 1257 | + *args, int or (int, int, int) or `SubplotSpec`, default: (1, 1, 1) |
| 1258 | + The position of the subplot described by one of |
| 1259 | +
|
| 1260 | + - Three integers (*nrows*, *ncols*, *index*). The subplot will |
| 1261 | + take the *index* position on a grid with *nrows* rows and |
| 1262 | + *ncols* columns. *index* starts at 1 in the upper left corner |
| 1263 | + and increases to the right. |
| 1264 | + - A 3-digit integer. The digits are interpreted as if given |
| 1265 | + separately as three single-digit integers, i.e. |
| 1266 | + ``fig.add_subplot(235)`` is the same as |
| 1267 | + ``fig.add_subplot(2, 3, 5)``. Note that this can only be used |
| 1268 | + if there are no more than 9 subplots. |
| 1269 | + - A `.SubplotSpec`. |
1270 | 1270 |
|
1271 | 1271 | In rare circumstances, `.add_subplot` may be called with a single |
1272 | 1272 | argument, a subplot axes instance already created in the |
@@ -1346,27 +1346,43 @@ def add_subplot(self, *args, **kwargs): |
1346 | 1346 | ax1.remove() # delete ax1 from the figure |
1347 | 1347 | fig.add_subplot(ax1) # add ax1 back to the figure |
1348 | 1348 | """ |
1349 | | - if not len(args): |
1350 | | - args = (1, 1, 1) |
1351 | | - |
1352 | | - if len(args) == 1 and isinstance(args[0], Integral): |
1353 | | - if not 100 <= args[0] <= 999: |
1354 | | - raise ValueError("Integer subplot specification must be a " |
1355 | | - "three-digit number, not {}".format(args[0])) |
1356 | | - args = tuple(map(int, str(args[0]))) |
1357 | | - |
1358 | 1349 | if 'figure' in kwargs: |
1359 | 1350 | # Axes itself allows for a 'figure' kwarg, but since we want to |
1360 | 1351 | # bind the created Axes to self, it is not allowed here. |
1361 | 1352 | raise TypeError( |
1362 | 1353 | "add_subplot() got an unexpected keyword argument 'figure'") |
1363 | 1354 |
|
1364 | | - if isinstance(args[0], SubplotBase): |
| 1355 | + nargs = len(args) |
| 1356 | + if nargs == 0: |
| 1357 | + args = (1, 1, 1) |
| 1358 | + elif nargs == 1: |
| 1359 | + if isinstance(args[0], Integral): |
| 1360 | + if not 100 <= args[0] <= 999: |
| 1361 | + raise ValueError(f"Integer subplot specification must be " |
| 1362 | + f"a three-digit number, not {args[0]}") |
| 1363 | + args = tuple(map(int, str(args[0]))) |
| 1364 | + elif isinstance(args[0], (SubplotBase, SubplotSpec)): |
| 1365 | + pass # no further validation or normalization needed |
| 1366 | + else: |
| 1367 | + raise TypeError('Positional arguments are not a valid ' |
| 1368 | + 'position specification.') |
| 1369 | + elif nargs == 3: |
| 1370 | + for arg in args: |
| 1371 | + if not isinstance(arg, Integral): |
| 1372 | + cbook.warn_deprecated( |
| 1373 | + "3.3", |
| 1374 | + message="Passing non-integers as three-element " |
| 1375 | + "position specification is deprecated.") |
| 1376 | + args = tuple(map(int, args)) |
| 1377 | + else: |
| 1378 | + raise TypeError(f'add_subplot() takes 1 or 3 positional arguments ' |
| 1379 | + f'but {nargs} were given') |
1365 | 1380 |
|
| 1381 | + if isinstance(args[0], SubplotBase): |
1366 | 1382 | ax = args[0] |
1367 | 1383 | if ax.get_figure() is not self: |
1368 | | - raise ValueError( |
1369 | | - "The Subplot must have been created in the present figure") |
| 1384 | + raise ValueError("The Subplot must have been created in " |
| 1385 | + "the present figure") |
1370 | 1386 | # make a key for the subplot (which includes the axes object id |
1371 | 1387 | # in the hash) |
1372 | 1388 | key = self._make_key(*args, **kwargs) |
|
0 commit comments