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

Skip to content

Commit 1486188

Browse files
committed
Deprecate auto-removal of overlapping Axes by plt.subplot{,2grid}.
In particular, note that the OO add_subplot does not have this behavior.
1 parent 99de892 commit 1486188

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Auto-removal of overlapping Axes by ``plt.subplot`` and ``plt.subplot2grid``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, `.pyplot.subplot` and `.pyplot.subplot2grid` would automatically
4+
remove preexisting Axes that overlap with the newly added Axes. This behavior
5+
was deemed confusing, and is now deprecated. Explicitly call ``ax.remove()``
6+
on Axes that need to be removed.

lib/matplotlib/pyplot.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,13 +1289,13 @@ def subplot(*args, **kwargs):
12891289

12901290
fig.sca(ax)
12911291

1292-
bbox = ax.bbox
1293-
axes_to_delete = []
1294-
for other_ax in fig.axes:
1295-
if other_ax == ax:
1296-
continue
1297-
if bbox.fully_overlaps(other_ax.bbox):
1298-
axes_to_delete.append(other_ax)
1292+
axes_to_delete = [other for other in fig.axes
1293+
if other != ax and ax.bbox.fully_overlaps(other.bbox)]
1294+
if axes_to_delete:
1295+
_api.warn_deprecated(
1296+
"3.6", message="Auto-removal of overlapping axes is deprecated "
1297+
"since %(since)s and will be removed %(removal)s; explicitly call "
1298+
"ax.remove() as needed.")
12991299
for ax_to_del in axes_to_delete:
13001300
delaxes(ax_to_del)
13011301

@@ -1582,13 +1582,14 @@ def subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs):
15821582

15831583
subplotspec = gs.new_subplotspec(loc, rowspan=rowspan, colspan=colspan)
15841584
ax = fig.add_subplot(subplotspec, **kwargs)
1585-
bbox = ax.bbox
1586-
axes_to_delete = []
1587-
for other_ax in fig.axes:
1588-
if other_ax == ax:
1589-
continue
1590-
if bbox.fully_overlaps(other_ax.bbox):
1591-
axes_to_delete.append(other_ax)
1585+
1586+
axes_to_delete = [other for other in fig.axes
1587+
if other != ax and ax.bbox.fully_overlaps(other.bbox)]
1588+
if axes_to_delete:
1589+
_api.warn_deprecated(
1590+
"3.6", message="Auto-removal of overlapping axes is deprecated "
1591+
"since %(since)s and will be removed %(removal)s; explicitly call "
1592+
"ax.remove() as needed.")
15921593
for ax_to_del in axes_to_delete:
15931594
delaxes(ax_to_del)
15941595

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ def test_inverted_cla():
440440
assert not ax.xaxis_inverted()
441441
assert ax.yaxis_inverted()
442442

443+
fig.clear()
443444
# 5. two shared axes. Inverting the leader axis should invert the shared
444445
# axes; clearing the leader axis should bring axes in shared
445446
# axes back to normal.

lib/matplotlib/tests/test_pyplot.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_subplot_replace_projection():
206206
ax = plt.subplot(1, 2, 1)
207207
ax1 = plt.subplot(1, 2, 1)
208208
ax2 = plt.subplot(1, 2, 2)
209-
# This will delete ax / ax1 as they fully overlap
209+
ax.remove()
210210
ax3 = plt.subplot(1, 2, 1, projection='polar')
211211
ax4 = plt.subplot(1, 2, 1, projection='polar')
212212
assert ax is not None
@@ -228,6 +228,7 @@ def test_subplot_kwarg_collision():
228228
ax1 = plt.subplot(projection='polar', theta_offset=0)
229229
ax2 = plt.subplot(projection='polar', theta_offset=0)
230230
assert ax1 is ax2
231+
ax1.remove()
231232
ax3 = plt.subplot(projection='polar', theta_offset=1)
232233
assert ax1 is not ax3
233234
assert ax1 not in plt.gcf().axes
@@ -274,13 +275,16 @@ def test_subplot_projection_reuse():
274275
assert ax1 is plt.gca()
275276
# make sure we get it back if we ask again
276277
assert ax1 is plt.subplot(111)
278+
# remove it
279+
ax1.remove()
277280
# create a polar plot
278281
ax2 = plt.subplot(111, projection='polar')
279282
assert ax2 is plt.gca()
280283
# this should have deleted the first axes
281284
assert ax1 not in plt.gcf().axes
282285
# assert we get it back if no extra parameters passed
283286
assert ax2 is plt.subplot(111)
287+
ax2.remove()
284288
# now check explicitly setting the projection to rectilinear
285289
# makes a new axes
286290
ax3 = plt.subplot(111, projection='rectilinear')
@@ -302,15 +306,19 @@ def test_subplot_polar_normalization():
302306

303307

304308
def test_subplot_change_projection():
309+
created_axes = set()
305310
ax = plt.subplot()
311+
created_axes.add(ax)
306312
projections = ('aitoff', 'hammer', 'lambert', 'mollweide',
307313
'polar', 'rectilinear', '3d')
308314
for proj in projections:
309-
ax_next = plt.subplot(projection=proj)
310-
assert ax_next is plt.subplot()
311-
assert ax_next.name == proj
312-
assert ax is not ax_next
313-
ax = ax_next
315+
ax.remove()
316+
ax = plt.subplot(projection=proj)
317+
assert ax is plt.subplot()
318+
assert ax.name == proj
319+
created_axes.add(ax)
320+
# Check that each call created a new Axes.
321+
assert len(created_axes) == 1 + len(projections)
314322

315323

316324
def test_polar_second_call():

0 commit comments

Comments
 (0)