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

Skip to content

Commit 2960490

Browse files
committed
FIX: restore creating new axes via plt.subplot with different kwargs
This adds a small amount of additional state to the Axes created via Figure.add_axes and Figure.add_subplot (which the other Axes creation methods eventually funnel through) to track the kwargs passed through. We then use that state in `pyplot.subplot` to determine if we should re-use an Axes found at a given position or create a new one (and implicitly destroy the existing one). closes #19432
1 parent ea68032 commit 2960490

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

lib/matplotlib/figure.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ def add_axes(self, *args, **kwargs):
576576
if not np.isfinite(rect).all():
577577
raise ValueError('all entries in rect must be finite '
578578
'not {}'.format(rect))
579-
projection_class, kwargs = self._process_projection_requirements(
579+
projection_class, pkw = self._process_projection_requirements(
580580
*args, **kwargs)
581581

582582
# create the new axes using the axes class given
583-
a = projection_class(self, rect, **kwargs)
584-
return self._add_axes_internal(a)
583+
a = projection_class(self, rect, **pkw)
584+
return self._add_axes_internal(a, kwargs)
585585

586586
@docstring.dedent_interpd
587587
def add_subplot(self, *args, **kwargs):
@@ -706,17 +706,18 @@ def add_subplot(self, *args, **kwargs):
706706
if (len(args) == 1 and isinstance(args[0], Integral)
707707
and 100 <= args[0] <= 999):
708708
args = tuple(map(int, str(args[0])))
709-
projection_class, kwargs = self._process_projection_requirements(
709+
projection_class, pkw = self._process_projection_requirements(
710710
*args, **kwargs)
711-
ax = subplot_class_factory(projection_class)(self, *args, **kwargs)
712-
return self._add_axes_internal(ax)
711+
ax = subplot_class_factory(projection_class)(self, *args, **pkw)
712+
return self._add_axes_internal(ax, kwargs)
713713

714-
def _add_axes_internal(self, ax):
714+
def _add_axes_internal(self, ax, input_kwargs):
715715
"""Private helper for `add_axes` and `add_subplot`."""
716716
self._axstack.push(ax)
717717
self._localaxes.push(ax)
718718
self.sca(ax)
719719
ax._remove_method = self.delaxes
720+
ax._init_kwargs = input_kwargs
720721
self.stale = True
721722
ax.stale_callback = _stale_figure_callback
722723
return ax

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,8 @@ def subplot(*args, **kwargs):
12291229
if hasattr(ax, 'get_subplotspec') and ax.get_subplotspec() == key),
12301230
None)
12311231

1232-
# If no existing axes match, then create a new one.
1233-
if ax is None:
1232+
# If no existing axes matches, then create a new one.
1233+
if ax is None or getattr(ax, '_init_kwargs', {}) != kwargs:
12341234
ax = fig.add_subplot(*args, **kwargs)
12351235

12361236
bbox = ax.bbox

lib/matplotlib/tests/test_figure.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,18 +1010,27 @@ def test_axes_kwargs():
10101010
plt.close()
10111011

10121012
# plt.subplot() searches for axes with the same subplot spec, and if one
1013-
# exists, returns it, regardless of whether the axes kwargs were the same.
1013+
# exists, and the kwargs match returns it, create a new one if they do not
10141014
fig = plt.figure()
10151015
ax = plt.subplot(1, 2, 1)
10161016
ax1 = plt.subplot(1, 2, 1)
10171017
ax2 = plt.subplot(1, 2, 2)
1018+
# This will delete ax / ax1 as they fully overlap
10181019
ax3 = plt.subplot(1, 2, 1, projection='polar')
1020+
ax4 = plt.subplot(1, 2, 1, projection='polar')
10191021
assert ax is not None
10201022
assert ax1 is ax
10211023
assert ax2 is not ax
1022-
assert ax3 is ax
1024+
assert ax3 is not ax
1025+
assert ax3 is ax4
1026+
1027+
assert ax not in fig.axes
1028+
assert ax2 in fig.axes
1029+
assert ax3 in fig.axes
1030+
10231031
assert ax.name == 'rectilinear'
1024-
assert ax3.name == 'rectilinear'
1032+
assert ax2.name == 'rectilinear'
1033+
assert ax3.name == 'polar'
10251034
plt.close()
10261035

10271036
# plt.gca() returns an existing axes, unless there were no axes.

0 commit comments

Comments
 (0)