Skip to content

Commit c81a965

Browse files
authored
Merge pull request matplotlib#15290 from anntzer/delaxes
Unify fig.delaxes(ax) and ax.remove().
2 parents 87495f5 + aafe0ef commit c81a965

File tree

3 files changed

+22
-37
lines changed

3 files changed

+22
-37
lines changed

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,11 +1163,8 @@ def set_alpha(self, alpha):
11631163
self.alpha = alpha
11641164

11651165
def remove(self):
1166-
"""
1167-
Remove this colorbar from the figure.
1168-
"""
1169-
fig = self.ax.figure
1170-
fig.delaxes(self.ax)
1166+
"""Remove this colorbar from the figure."""
1167+
self.ax.remove()
11711168

11721169

11731170
class Colorbar(ColorbarBase):

lib/matplotlib/figure.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,16 +1021,6 @@ def set_frameon(self, b):
10211021

10221022
frameon = property(get_frameon, set_frameon)
10231023

1024-
def delaxes(self, ax):
1025-
"""
1026-
Remove the `~matplotlib.axes.Axes` *ax* from the figure and update the
1027-
current axes.
1028-
"""
1029-
self._axstack.remove(ax)
1030-
for func in self._axobservers:
1031-
func(self)
1032-
self.stale = True
1033-
10341024
def add_artist(self, artist, clip=False):
10351025
"""
10361026
Add any :class:`~matplotlib.artist.Artist` to the figure.
@@ -1357,25 +1347,17 @@ def add_subplot(self, *args, **kwargs):
13571347
::
13581348
13591349
fig = plt.figure()
1360-
fig.add_subplot(221)
1361-
1362-
# equivalent but more general
1363-
ax1 = fig.add_subplot(2, 2, 1)
1364-
1365-
# add a subplot with no frame
1366-
ax2 = fig.add_subplot(222, frameon=False)
13671350
1368-
# add a polar subplot
1369-
fig.add_subplot(223, projection='polar')
1351+
fig.add_subplot(231)
1352+
ax1 = fig.add_subplot(2, 3, 1) # equivalent but more general
13701353
1371-
# add a red subplot that share the x-axis with ax1
1372-
fig.add_subplot(224, sharex=ax1, facecolor='red')
1354+
fig.add_subplot(232, frameon=False) # subplot with no frame
1355+
fig.add_subplot(233, projection='polar') # polar subplot
1356+
fig.add_subplot(234, sharex=ax1) # subplot sharing x-axis with ax1
1357+
fig.add_subplot(235, facecolor="red") # red subplot
13731358
1374-
#delete x2 from the figure
1375-
fig.delaxes(ax2)
1376-
1377-
#add x2 to the figure again
1378-
fig.add_subplot(ax2)
1359+
ax1.remove() # delete ax1 from the figure
1360+
fig.add_subplot(ax1) # add ax1 back to the figure
13791361
"""
13801362
if not len(args):
13811363
args = (1, 1, 1)
@@ -1429,7 +1411,7 @@ def _add_axes_internal(self, key, ax):
14291411
"""Private helper for `add_axes` and `add_subplot`."""
14301412
self._axstack.add(key, ax)
14311413
self.sca(ax)
1432-
ax._remove_method = self._remove_ax
1414+
ax._remove_method = self.delaxes
14331415
self.stale = True
14341416
ax.stale_callback = _stale_figure_callback
14351417
return ax
@@ -1602,7 +1584,11 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
16021584
# Returned axis array will be always 2-d, even if nrows=ncols=1.
16031585
return axarr
16041586

1605-
def _remove_ax(self, ax):
1587+
def delaxes(self, ax):
1588+
"""
1589+
Remove the `~.axes.Axes` *ax* from the figure; update the current axes.
1590+
"""
1591+
16061592
def _reset_locators_and_formatters(axis):
16071593
# Set the formatters and locators to be associated with axis
16081594
# (where previously they may have been associated with another
@@ -1644,7 +1630,11 @@ def _break_share_link(ax, grouper):
16441630
return last_ax
16451631
return None
16461632

1647-
self.delaxes(ax)
1633+
self._axstack.remove(ax)
1634+
for func in self._axobservers:
1635+
func(self)
1636+
self.stale = True
1637+
16481638
last_ax = _break_share_link(ax, ax._shared_y_axes)
16491639
if last_ax is not None:
16501640
_reset_locators_and_formatters(last_ax.yaxis)

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,12 +803,10 @@ def axes(arg=None, **kwargs):
803803
def delaxes(ax=None):
804804
"""
805805
Remove the `Axes` *ax* (defaulting to the current axes) from its figure.
806-
807-
A KeyError is raised if the axes doesn't exist.
808806
"""
809807
if ax is None:
810808
ax = gca()
811-
ax.figure.delaxes(ax)
809+
ax.remove()
812810

813811

814812
def sca(ax):

0 commit comments

Comments
 (0)