Skip to content

Commit e6df62e

Browse files
committed
MNT: Remove deprecated axes kwargs collision detection
In Matplotlib 2.1, the behavior of reusing existing axes when created with the same arguments was deprecated (see #9037). This behavior is now removed. The behavior of the functions to create new axes (`pyplot.axes`, `pyplot.subplot`, `figure.Figure.add_axes`, `figure.Figure.add_subplot`) has changed. In the past, these functions would detect if you were attempting to create Axes with the same keyword arguments as already-existing axes in the current figure, and if so, they would return the existing Axes. Now, these functions will always create new Axes. A special exception is `pyplot.subplot`, which will reuse any existing subplot with a matching subplot spec. However, if there is a subplot with a matching subplot spec, then that subplot will be returned, even if the keyword arguments with which it was created differ. Correspondingly, the behavior of the functions to get the current Axes (`pyplot.gca`, `figure.Figure.gca`) has changed. In the past, these functions accepted keyword arguments. If the keyword arguments matched an already-existing Axes, then that Axes would be returned, otherwise new Axes would be created with those keyword arguments. Now, the keyword arguments are only considered if there are no axes at all in the current figure. In a future release, these functions will not accept keyword arguments at all. Fixes #18832.
1 parent 09ab2c2 commit e6df62e

File tree

9 files changed

+126
-279
lines changed

9 files changed

+126
-279
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pyplot.gca()
2+
~~~~~~~~~~~~
3+
4+
Passing keyword arguments to ``.pyplot.gca`` will not be supported in a future
5+
release.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``figure._AxesStack``
2+
~~~~~~~~~~~~~~~~~~~~~
3+
This internal class has been removed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Changes to behavior of Axes creation methods (gca(), add_axes(), add_subplot())
2+
-------------------------------------------------------------------------------
3+
4+
The behavior of the functions to create new axes (``.pyplot.axes``,
5+
``.pyplot.subplot``, ``.figure.Figure.add_axes``,
6+
``.figure.Figure.add_subplot``) has changed. In the past, these functions would
7+
detect if you were attempting to create Axes with the same keyword arguments as
8+
already-existing axes in the current figure, and if so, they would return the
9+
existing Axes. Now, these functions will always create new Axes. A special
10+
exception is ``.pyplot.subplot``, which will reuse any existing subplot with a
11+
matching subplot spec. However, if there is a subplot with a matching subplot
12+
spec, then that subplot will be returned, even if the keyword arguments with
13+
which it was created differ.
14+
15+
Correspondingly, the behavior of the functions to get the current Axes
16+
(``.pyplot.gca``, ``.figure.Figure.gca``) has changed. In the past, these
17+
functions accepted keyword arguments. If the keyword arguments matched an
18+
already-existing Axes, then that Axes would be returned, otherwise new Axes
19+
would be created with those keyword arguments. Now, the keyword arguments are
20+
only considered if there are no axes at all in the current figure. In a future
21+
release, these functions will not accept keyword arguments at all.

lib/matplotlib/axes/_subplots.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import uuid
32

43
from matplotlib import _api, cbook, docstring
54
import matplotlib.artist as martist
@@ -142,16 +141,7 @@ def _make_twin_axes(self, *args, **kwargs):
142141
# which currently uses this internal API.
143142
if kwargs["sharex"] is not self and kwargs["sharey"] is not self:
144143
raise ValueError("Twinned Axes may share only one axis")
145-
# The dance here with label is to force add_subplot() to create a new
146-
# Axes (by passing in a label never seen before). Note that this does
147-
# not affect plot reactivation by subplot() as twin axes can never be
148-
# reactivated by subplot().
149-
sentinel = str(uuid.uuid4())
150-
real_label = kwargs.pop("label", sentinel)
151-
twin = self.figure.add_subplot(
152-
self.get_subplotspec(), *args, label=sentinel, **kwargs)
153-
if real_label is not sentinel:
154-
twin.set_label(real_label)
144+
twin = self.figure.add_subplot(self.get_subplotspec(), *args, **kwargs)
155145
self.set_adjustable('datalim')
156146
twin.set_adjustable('datalim')
157147
self._twinned_axes.join(self, twin)

lib/matplotlib/cbook/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ def __len__(self):
596596
def __getitem__(self, ind):
597597
return self._elements[ind]
598598

599+
def as_list(self):
600+
return list(self._elements)
601+
599602
def forward(self):
600603
"""Move the position forward and return the current element."""
601604
self._pos = min(self._pos + 1, len(self._elements) - 1)

0 commit comments

Comments
 (0)