Skip to content

Commit 385e1a5

Browse files
committed
FIX: set sca when re-using as Axes with pyplot.subplot
1 parent 2960490 commit 385e1a5

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ def subplot(*args, **kwargs):
12321232
# If no existing axes matches, then create a new one.
12331233
if ax is None or getattr(ax, '_init_kwargs', {}) != kwargs:
12341234
ax = fig.add_subplot(*args, **kwargs)
1235+
fig.sca(ax)
12351236

12361237
bbox = ax.bbox
12371238
axes_to_delete = []

lib/matplotlib/tests/test_pyplot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,12 @@ def test_nested_ion_ioff():
153153
with plt.ioff():
154154
plt.ion()
155155
assert not mpl.is_interactive()
156+
157+
158+
def test_subplot_reuse():
159+
ax1 = plt.subplot(121)
160+
assert ax1 is plt.gca()
161+
ax2 = plt.subplot(122)
162+
assert ax2 is plt.gca()
163+
ax1 = plt.subplot(121)
164+
assert ax1 is plt.gca()

0 commit comments

Comments
 (0)