8000 FIX: allow start-stop subplot · matplotlib/matplotlib@909e560 · GitHub
[go: up one dir, main page]

Skip to content

Commit 909e560

Browse files
committed
FIX: allow start-stop subplot
1 parent 969513e commit 909e560

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

lib/matplotlib/figure.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,16 +1265,27 @@ def add_subplot(self, *args, **kwargs):
12651265
12661266
Parameters
12671267
----------
1268+
<<<<<<< HEAD
12681269
*args : int, (int, int, *index*), or `.SubplotSpec`, default: (1, 1, 1)
1270+
=======
1271+
*args, int, (int, int, *index*), or `SubplotSpec`, default: (1, 1, 1)
1272+
>>>>>>> FIX: allow start-stop subplot
12691273
The position of the subplot described by one of
12701274
12711275
- Three integers (*nrows*, *ncols*, *index*). The subplot will
12721276
take the *index* position on a grid with *nrows* rows and
12731277
*ncols* columns. *index* starts at 1 in the upper left corner
1278+
<<<<<<< HEAD
12741279
and increases to the right. *index* can also be a two-tuple
12751280
specifying the (*first*, *last*) indices (1-based, and including
12761281
*last*) of the subplot, e.g., ``fig.add_subplot(3, 1, (1, 2))``
12771282
makes a subplot that spans the upper 2/3 of the figure.
1283+
=======
1284+
and increases to the right. *index* can also be a two-tuple
1285+
specifying the (*start*, *stop*) indices of the subplot, i.e.,
1286+
``fig.add_subplot(3, 1, (1, 2))`` makes a subplot that spans the
1287+
upper 2/3 of the figure.
1288+
>>>>>>> FIX: allow start-stop subplot
12781289
- A 3-digit integer. The digits are interpreted as if given
12791290
separately as three single-digit integers, i.e.
12801291
``fig.add_subplot(235)`` is the same as
@@ -1366,7 +1377,46 @@ def add_subplot(self, *args, **kwargs):
13661377
raise TypeError(
13671378
"add_subplot() got an unexpected keyword argument 'figure'")
13681379

1369-
if len(args) == 1 and isinstance(args[0], SubplotBase):
1380+
nargs = len(args)
1381+
if nargs == 0:
1382+
args = (1, 1, 1)
1383+
elif nargs == 1:
1384+
if isinstance(args[0], Integral):
1385+
if not 100 <= args[0] <= 999:
1386+
raise ValueError(f"Integer subplot specification must be "
1387+
f"a three-digit number, not {args[0]}")
1388+
args = tuple(map(int, str(args[0])))
1389+
elif isinstance(args[0], (SubplotBase, SubplotSpec)):
1390+
pass # no further validation or normalization needed
1391+
else:
1392+
raise TypeError('Positional arguments are not a valid '
1393+
'position specification.')
1394+
elif nargs == 3:
1395+
newarg = [None] * 3
1396+
message = ("Passing non-integers as three-element "
1397+
"position specification is deprecated since "
1398+
"%(since)s and will be removed %(removal)s.")
1399+
for nn, arg in enumerate(args[:2]):
1400+
if not isinstance(arg, Integral):
1401+
cbook.warn_deprecated("3.3", message=message)
1402+
newarg[nn] = int(arg)
1403+
args2 = args[2]
1404+
if isinstance(args[2], tuple) and len(args[2]) == 2:
1405+
# start/stop two-tuple is allowed...
1406+
for arg in args[2]:
1407+
if not isinstance(arg, Integral):
1408+
cbook.warn_deprecated("3.3", message=message)
1409+
newarg[2] = (int(args[2][0]), int(args[2][1]))
1410+
else:
1411+
if not isinstance(args[2], Integral):
1412+
cbook.warn_deprecated("3.3", message=message)
1413+
newarg[2] = int(args[2])
1414+
args = tuple(newarg)
1415+
else:
1416+
raise TypeError(f'add_subplot() takes 1 or 3 positional arguments '
1417+
f'but {nargs} were given')
1418+
1419+
if isinstance(args[0], SubplotBase):
13701420
ax = args[0]
13711421
if ax.get_figure() is not self:
13721422
raise ValueError("The Subplot must have been created in "

lib/matplotlib/tests/test_figure.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,16 @@ def test_add_subplot_twotuple():
566566
assert ax4.get_subplotspec().colspan == range(0, 2)
567567
with pytest.raises(IndexError):
568568
fig.add_subplot(3, 2, (6, 3))
569+
570+
571+
def test_add_subplot_twotuple():
572+
fig = plt.figure()
573+
ax2 = fig.add_subplot(3, 2, (3, 5))
574+
assert ax2.get_subplotspec().rowspan == range(1, 3)
575+
assert ax2.get_subplotspec().colspan == range(0, 1)
576+
ax3 = fig.add_subplot(3, 2, (4, 6))
577+
assert ax3.get_subplotspec().rowspan == range(1, 3)
578+
assert ax3.get_subplotspec().colspan == range(1, 2)
579+
ax4 = fig.add_subplot(3, 2, (3, 6))
580+
assert ax4.get_subplotspec().rowspan == range(1, 3)
581+
assert ax4.get_subplotspec().colspan == range(0, 2)

0 commit comments

Comments
 (0)
0