Skip to content

Always create a new subplot in plt.subplot() #19434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions doc/users/next_whats_new/axes_kwargs_collision.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ The behavior of the functions to create new axes (`.pyplot.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.
existing Axes. Now, these functions will always create new Axes.

Correspondingly, the behavior of the functions to get the current Axes
(`.pyplot.gca`, `.figure.Figure.gca`) has changed. In the past, these functions
Expand Down
21 changes: 1 addition & 20 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,15 +1156,6 @@ def subplot(*args, **kwargs):
If you do not want this behavior, use the `.Figure.add_subplot` method
or the `.pyplot.axes` function instead.

If the figure already has a subplot with key (*args*,
*kwargs*) then it will simply make that subplot current and
return it. This behavior is deprecated. Meanwhile, if you do
not want this behavior (i.e., you want to force the creation of a
new subplot), you must use a unique set of args and kwargs. The axes
*label* attribute has been exposed for this purpose: if you want
two subplots that are otherwise identical to be added to the figure,
make sure you give them unique labels.

In rare circumstances, `.Figure.add_subplot` may be called with a single
argument, a subplot axes instance already created in the
present figure but not in the figure's list of axes.
Expand Down Expand Up @@ -1221,17 +1212,7 @@ def subplot(*args, **kwargs):
"and/or 'nrows'. Did you intend to call subplots()?")

fig = gcf()

# First, search for an existing subplot with a matching spec.
key = SubplotSpec._from_subplot_args(fig, args)
ax = next(
(ax for ax in fig.axes
if hasattr(ax, 'get_subplotspec') and ax.get_subplotspec() == key),
None)

# If no existing axes match, then create a new one.
if ax is None:
ax = fig.add_subplot(*args, **kwargs)
ax = fig.add_subplot(*args, **kwargs)

bbox = ax.bbox
axes_to_delete = []
Expand Down