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

Skip to content

Commit 040e86d

Browse files
committed
FIX: set sca when re-using as Axes with pyplot.subplot
1 parent 5b95065 commit 040e86d

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
@@ -1233,6 +1233,7 @@ def subplot(*args, **kwargs):
12331233
# If no existing axes matches, then create a new one.
12341234
if ax is None or getattr(ax, '_projection_init', ()) != key:
12351235
ax = fig.add_subplot(*args, **kwargs)
1236+
fig.sca(ax)
12361237

12371238
bbox = ax.bbox
12381239
axes_to_delete = []

lib/matplotlib/tests/test_pyplot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,12 @@ def test_close():
161161
except TypeError as e:
162162
assert str(e) == "close() argument must be a Figure, an int, " \
163163
"a string, or None, not <class 'float'>"
164+
165+
166+
def test_subplot_reuse():
167+
ax1 = plt.subplot(121)
168+
assert ax1 is plt.gca()
169+
ax2 = plt.subplot(122)
170+
assert ax2 is plt.gca()
171+
ax1 = plt.subplot(121)
172+
assert ax1 is plt.gca()

0 commit comments

Comments
 (0)