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

Skip to content

Commit be1b730

Browse files
committed
FIX: allow start-stop subplot
1 parent f843ffd commit be1b730

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

lib/matplotlib/figure.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,13 +1264,16 @@ def add_subplot(self, *args, **kwargs):
12641264
12651265
Parameters
12661266
----------
1267-
*args, int or (int, int, int) or `SubplotSpec`, default: (1, 1, 1)
1267+
*args, int, (int, int, *index*), or `SubplotSpec`, default: (1, 1, 1)
12681268
The position of the subplot described by one of
12691269
12701270
- Three integers (*nrows*, *ncols*, *index*). The subplot will
12711271
take the *index* position on a grid with *nrows* rows and
12721272
*ncols* columns. *index* starts at 1 in the upper left corner
1273-
and increases to the right.
1273+
and increases to the right. *index* can also be a two-tuple
1274+
specifying the (*start*, *stop*) indices of the subplot, i.e.,
1275+
``fig.add_subplot(3, 1, (1, 2))`` makes a subplot that spans the
1276+
upper 2/3 of the figure.
12741277
- A 3-digit integer. The digits are interpreted as if given
12751278
separately as three single-digit integers, i.e.
12761279
``fig.add_subplot(235)`` is the same as
@@ -1377,14 +1380,26 @@ def add_subplot(self, *args, **kwargs):
13771380
raise TypeError('Positional arguments are not a valid '
13781381
'position specification.')
13791382
elif nargs == 3:
1380-
for arg in args:
1383+
newarg = [None] * 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+
for nn, arg in enumerate(args[:2]):
13811388
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))
1389+
cbook.warn_deprecated("3.3", message=message)
1390+
newarg[nn] = int(arg)
1391+
args2 = args[2]
1392+
if isinstance(args2, tuple) and len(args2) == 2:
1393+
# start/stop two-tuple is allowed...
1394+
for arg in args2:
1395+
if not isinstance(arg, Integral):
1396+
cbook.warn_deprecated("3.3", message=message)
1397+
newarg[2] = (int(args2[0]), int(args2[1]))
1398+
else:
1399+
if not isinstance(args2, Integral):
1400+
cbook.warn_deprecated("3.3", message=message)
1401+
newarg[2] = int(args2)
1402+
args = tuple(newarg)
13881403
else:
13891404
raise TypeError(f'add_subplot() takes 1 or 3 positional arguments '
13901405
f'but {nargs} were given')

lib/matplotlib/tests/test_figure.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,16 @@ def test_removed_axis():
533533
fig, axs = plt.subplots(2, sharex=True)
534534
axs[0].remove()
535535
fig.canvas.draw()
536+
537+
538+
def test_add_subplot_twotuple():
539+
fig = plt.figure()
540+
ax2 = fig.add_subplot(3, 2, (3, 5))
541+
assert ax2.get_subplotspec().rowspan == range(1, 3)
542+
assert ax2.get_subplotspec().colspan == range(0, 1)
543+
ax3 = fig.add_subplot(3, 2, (4, 6))
544+
assert ax3.get_subplotspec().rowspan == range(1, 3)
545+
assert ax3.get_subplotspec().colspan == range(1, 2)
546+
ax4 = fig.add_subplot(3, 2, (3, 6))
547+
assert ax4.get_subplotspec().rowspan == range(1, 3)
548+
assert ax4.get_subplotspec().colspan == range(0, 2)

0 commit comments

Comments
 (0)
0