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