Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a777703

Browse files
committed
FIX
1 parent b36b3f6 commit a777703

File tree

2 files changed

+19
-72
lines changed

2 files changed

+19
-72
lines changed

lib/matplotlib/figure.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ def show(self, warn=True):
384384
AttributeError.
385385
386386
.. warning::
387+
387388
This does not manage an GUI event loop. Consequently, the figure
388389
may only be shown briefly or not shown at all if you or your
389390
environment are not managing an event loop.
@@ -1264,13 +1265,16 @@ def add_subplot(self, *args, **kwargs):
12641265
12651266
Parameters
12661267
----------
1267-
*args, int or (int, int, int) or `SubplotSpec`, default: (1, 1, 1)
1268+
*args : int, (int, int, *index*), or `.SubplotSpec`, default: (1, 1, 1)
12681269
The position of the subplot described by one of
12691270
12701271
- Three integers (*nrows*, *ncols*, *index*). The subplot will
12711272
take the *index* position on a grid with *nrows* rows and
12721273
*ncols* columns. *index* starts at 1 in the upper left corner
1273-
and increases to the right.
1274+
and increases to the right. *index* can also be a two-tuple
1275+
specifying the (*first*, *last*) indices (1-based, and including
1276+
*last*) of the subplot, e.g., ``fig.add_subplot(3, 1, (1, 2))``
1277+
makes a subplot that spans the upper 2/3 of the figure.
12741278
- A 3-digit integer. The digits are interpreted as if given
12751279
separately as three single-digit integers, i.e.
12761280
``fig.add_subplot(235)`` is the same as
@@ -1362,48 +1366,27 @@ def add_subplot(self, *args, **kwargs):
13621366
raise TypeError(
13631367
"add_subplot() got an unexpected keyword argument 'figure'")
13641368

1365-
nargs = len(args)
1366-
if nargs == 0:
1367-
args = (1, 1, 1)
1368-
elif nargs == 1:
1369-
if isinstance(args[0], Integral):
1370-
if not 100 <= args[0] <= 999:
1371-
raise ValueError(f"Integer subplot specification must be "
1372-
f"a three-digit number, not {args[0]}")
1373-
args = tuple(map(int, str(args[0])))
1374-
elif isinstance(args[0], (SubplotBase, SubplotSpec)):
1375-
pass # no further validation or normalization needed
1376-
else:
1377-
raise TypeError('Positional arguments are not a valid '
1378-
'position specification.')
1379-
elif nargs == 3:
1380-
for arg in args:
1381-
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))
1388-
else:
1389-
raise TypeError(f'add_subplot() takes 1 or 3 positional arguments '
1390-
f'but {nargs} were given')
1391-
1392-
if isinstance(args[0], SubplotBase):
1369+
if len(args) == 1 and isinstance(args[0], SubplotBase):
13931370
ax = args[0]
13941371
if ax.get_figure() is not self:
13951372
raise ValueError("The Subplot must have been created in "
13961373
"the present figure")
13971374
# make a key for the subplot (which includes the axes object id
13981375
# in the hash)
13991376
key = self._make_key(*args, **kwargs)
1377+
14001378
else:
1379+
if not args:
1380+
args = (1, 1, 1)
1381+
# Normalize correct ijk values to (i, j, k) here so that
1382+
# add_subplot(111) == add_subplot(1, 1, 1). Invalid values will
1383+
# trigger errors later (via SubplotSpec._from_subplot_args).
1384+
if (len(args) == 1 and isinstance(args[0], Integral)
1385+
and 100 <= args[0] <= 999):
1386+
args = tuple(map(int, str(args[0])))
14011387
projection_class, kwargs, key = \
14021388
self._process_projection_requirements(*args, **kwargs)
1403-
1404-
# try to find the axes with this key in the stack
1405-
ax = self._axstack.get(key)
1406-
1389+
ax = self._axstack.get(key) # search axes with this key in stack
14071390
if ax is not None:
14081391
if isinstance(ax, projection_class):
14091392
# the axes already existed, so set it as active & return
@@ -1416,7 +1399,6 @@ def add_subplot(self, *args, **kwargs):
14161399
# Without this, add_subplot would be simpler and
14171400
# more similar to add_axes.
14181401
self._axstack.remove(ax)
1419-
14201402
ax = subplot_class_factory(projection_class)(self, *args, **kwargs)
14211403

14221404
return self._add_axes_internal(key, ax)

lib/matplotlib/tests/test_figure.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ def test_add_subplot_invalid():
190190
with pytest.raises(ValueError, match='num must be 1 <= num <= 4'):
191191
fig.add_subplot(2, 2, 5)
192192

193-
with pytest.raises(ValueError, match='Integer subplot specification must'):
193+
with pytest.raises(ValueError, match='must be a three-digit integer'):
194194
fig.add_subplot(42)
195-
with pytest.raises(ValueError, match='Integer subplot specification must'):
195+
with pytest.raises(ValueError, match='must be a three-digit integer'):
196196
fig.add_subplot(1000)
197197

198198
with pytest.raises(TypeError, match='takes 1 or 3 positional arguments '
@@ -566,38 +566,3 @@ 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)
582-
583-
584-
def test_reuse_gridspec():
585-
fig = plt.figure()
586-
ax = [None] * 9
587-
for i in range(9):
588-
ax[i] = fig.add_subplot(3, 3, i + 1)
589-
for i in range(1, 9):
590-
assert (ax[i].get_subplotspec().get_gridspec() ==
591-
ax[0].get_subplotspec().get_gridspec())
592-
593-
fig = plt.figure()
594-
ax1 = fig.add_subplot(3, 3, (1, 7))
595-
ax2 = fig.add_subplot(3, 3, (6, 9))
596-
assert (ax1.get_subplotspec().get_gridspec() ==
597-
ax2.get_subplotspec().get_gridspec())
598-
599-
fig = plt.figure()
600-
ax1 = plt.subplot2grid((3, 3), (0, 0))
601-
ax2 = plt.subplot2grid((3, 3), (1, 1), colspan=2)
602-
assert (ax1.get_subplotspec().get_gridspec() ==
603-
ax2.get_subplotspec().get_gridspec())

0 commit comments

Comments
 (0)