8000 Unify fig.delaxes(ax) and ax.remove(). · matplotlib/matplotlib@358c651 · GitHub
[go: up one dir, main page]

Skip to content

Commit 358c651

Browse files
committed
Unify fig.delaxes(ax) and ax.remove().
fig.delaxes(ax) and ax.remove() are subtly different because the former fails to correctly reset locators and formatters when removing shared axes. Fix that by putting the logic in delaxes and making ax.remove() just map to it.
1 parent 4b27b46 commit 358c651

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
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: 13 additions & 15 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.
@@ -1366,10 +1356,10 @@ def add_subplot(self, *args, **kwargs):
13661356
# add a red subplot that share the x-axis with ax1
13671357
fig.add_subplot(224, sharex=ax1, facecolor='red')
13681358
1369-
#delete x2 from the figure
1359+
# delete x2 from the figure
13701360
fig.delaxes(ax2)
13711361
1372-
#add x2 to the figure again
1362+
# add x2 to the figure again
13731363
fig.add_subplot(ax2)
13741364
"""
13751365
if not len(args):
@@ -1424,7 +1414,7 @@ def _add_axes_internal(self, key, ax):
14241414
"""Private helper for `add_axes` and `add_subplot`."""
14251415
self._axstack.add(key, ax)
14261416
self.sca(ax)
1427-
ax._remove_method = self._remove_ax
1417+
ax._remove_method = self.delaxes
14281418
self.stale = True
14291419
ax.stale_callback = _stale_figure_callback
14301420
return ax
@@ -1597,7 +1587,11 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
15971587
# Returned axis array will be always 2-d, even if nrows=ncols=1.
15981588
return axarr
15991589

1600-
def _remove_ax(self, ax):
1590+
def delaxes(self, ax):
1591+
"""
1592+
Remove the `~.axes.Axes` *ax* from the figure; update the current axes.
1593+
"""
1594+
16011595
def _reset_locators_and_formatters(axis):
16021596
# Set the formatters and locators to be associated with axis
16031597
# (where previously they may have been associated with another
@@ -1639,7 +1633,11 @@ def _break_share_link(ax, grouper):
16391633
return last_ax
16401634
return None
16411635

1642-
self.delaxes(ax)
1636+
self._axstack.remove(ax)
1637+
for func in self._axobservers:
1638+
func(self)
1639+
self.stale = True
1640+
16431641
last_ax = _break_share_link(ax, ax._shared_y_axes)
16441642
if last_ax is not None:
16451643
_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
@@ -828,12 +828,10 @@ def axes(arg=None, **kwargs):
828828
def delaxes(ax=None):
829829
"""
830830
Remove the `Axes` *ax* (defaulting to the current axes) from its figure.
831-
832-
A KeyError is raised if the axes doesn't exist.
833831
"""
834832
if ax is None:
835833
ax = gca()
836-
ax.figure.delaxes(ax)
834+
ax.remove()
837835

838836

839837
def sca(ax):

0 commit comments

Comments
 (0)
0