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

Skip to content

Commit f57292b

Browse files
authored
Merge pull request #25859 from QuLogic/add_axes-extra
Deprecate passing extra arguments to Figure.add_axes
2 parents 544d8ce + 6dbd257 commit f57292b

File tree

6 files changed

+31
-16
lines changed

6 files changed

+31
-16
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Passing extra positional arguments to ``Figure.add_axes``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Positional arguments passed to `.Figure.add_axes` other than a rect or an
5+
existing ``Axes`` are currently ignored, and doing so is now deprecated.

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
392392
# This puts the rectangle into figure-relative coordinates.
393393
inset_locator = _TransformedBoundsLocator(bounds, transform)
394394
bounds = inset_locator(self, None).bounds
395-
projection_class, pkw = self.figure._process_projection_requirements(
396-
bounds, **kwargs)
395+
projection_class, pkw = self.figure._process_projection_requirements(**kwargs)
397396
inset_ax = projection_class(self.figure, bounds, zorder=zorder, **pkw)
398397

399398
# this locator lets the axes move if in data coordinates.

lib/matplotlib/figure.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -621,22 +621,26 @@ def add_axes(self, *args, **kwargs):
621621
args = (kwargs.pop('rect'), )
622622

623623
if isinstance(args[0], Axes):
624-
a = args[0]
624+
a, *extra_args = args
625625
key = a._projection_init
626626
if a.get_figure() is not self:
627627
raise ValueError(
628628
"The Axes must have been created in the present figure")
629629
else:
630-
rect = args[0]
630+
rect, *extra_args = args
631631
if not np.isfinite(rect).all():
632-
raise ValueError('all entries in rect must be finite '
633-
f'not {rect}')
634-
projection_class, pkw = self._process_projection_requirements(
635-
*args, **kwargs)
632+
raise ValueError(f'all entries in rect must be finite not {rect}')
633+
projection_class, pkw = self._process_projection_requirements(**kwargs)
636634

637635
# create the new axes using the axes class given
638636
a = projection_class(self, rect, **pkw)
639637
key = (projection_class, pkw)
638+
639+
if extra_args:
640+
_api.warn_deprecated(
641+
"3.8",
642+
name="Passing more than one positional argument to Figure.add_axes",
643+
addendum="Any additional positional arguments are currently ignored.")
640644
return self._add_axes_internal(a, key)
641645

642646
@_docstring.dedent_interpd
@@ -762,8 +766,7 @@ def add_subplot(self, *args, **kwargs):
762766
if (len(args) == 1 and isinstance(args[0], Integral)
763767
and 100 <= args[0] <= 999):
764768
args = tuple(map(int, str(args[0])))
765-
projection_class, pkw = self._process_projection_requirements(
766-
*args, **kwargs)
769+
projection_class, pkw = self._process_projection_requirements(**kwargs)
767770
ax = projection_class(self, *args, **pkw)
768771
key = (projection_class, pkw)
769772
return self._add_axes_internal(ax, key)
@@ -1662,9 +1665,8 @@ def _gci(self):
16621665
return im
16631666
return None
16641667

1665-
def _process_projection_requirements(
1666-
self, *args, axes_class=None, polar=False, projection=None,
1667-
**kwargs):
1668+
def _process_projection_requirements(self, *, axes_class=None, polar=False,
1669+
projection=None, **kwargs):
16681670
"""
16691671
Handle the args/kwargs to add_axes/add_subplot/gca, returning::
16701672

lib/matplotlib/figure.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class FigureBase(Artist):
232232
def gca(self) -> Axes: ...
233233
def _gci(self) -> ScalarMappable | None: ...
234234
def _process_projection_requirements(
235-
self, *args, axes_class=None, polar=False, projection=None, **kwargs
235+
self, *, axes_class=None, polar=False, projection=None, **kwargs
236236
) -> tuple[type[Axes], dict[str, Any]]: ...
237237
def get_default_bbox_extra_artists(self) -> list[Artist]: ...
238238
def get_tightbbox(

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ def subplot(*args, **kwargs) -> Axes:
13941394
if (ax.get_subplotspec() == key
13951395
and (kwargs == {}
13961396
or (ax._projection_init
1397-
== fig._process_projection_requirements(*args, **kwargs)))):
1397+
== fig._process_projection_requirements(**kwargs)))):
13981398
break
13991399
else:
14001400
# we have exhausted the known Axes and none match, make a new one!

lib/matplotlib/tests/test_figure.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,21 @@ def test_invalid_figure_add_axes():
485485
with pytest.raises(TypeError, match="multiple values for argument 'rect'"):
486486
fig.add_axes([0, 0, 1, 1], rect=[0, 0, 1, 1])
487487

488-
_, ax = plt.subplots()
488+
fig2, ax = plt.subplots()
489489
with pytest.raises(ValueError,
490490
match="The Axes must have been created in the present "
491491
"figure"):
492492
fig.add_axes(ax)
493493

494+
fig2.delaxes(ax)
495+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
496+
match="Passing more than one positional argument"):
497+
fig2.add_axes(ax, "extra positional argument")
498+
499+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
500+
match="Passing more than one positional argument"):
501+
fig.add_axes([0, 0, 1, 1], "extra positional argument")
502+
494503

495504
def test_subplots_shareax_loglabels():
496505
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, squeeze=False)

0 commit comments

Comments
 (0)