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