Skip to content

Commit 0245db9

Browse files
committed
FIX/ENH: do more clean up when removing axes
This adds clean up of the shared axes `Grouper` objects as part of removing an axes. `_reset_loc_form` is required because the locator/formatters end up bound to the axis objects of the last axes object (all of the Axes in the group share the same formatter/locator objects but have different Axis objects so that all but one of the Axis objects can be made not visible to not over-draw). If these are not re-bound to a still visible axes then changing the limits will change the view limits, but not the tick locations. closes #5663
1 parent db34cb7 commit 0245db9

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lib/matplotlib/figure.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ def add_axes(self, *args, **kwargs):
916916

917917
self._axstack.add(key, a)
918918
self.sca(a)
919-
a._remove_method = lambda ax: self.delaxes(ax)
919+
a._remove_method = self.__remove_ax
920920
self.stale = True
921921
a.stale_callback = _stale_figure_callback
922922
return a
@@ -1006,11 +1006,37 @@ def add_subplot(self, *args, **kwargs):
10061006

10071007
self._axstack.add(key, a)
10081008
self.sca(a)
1009-
a._remove_method = lambda ax: self.delaxes(ax)
1009+
a._remove_method = self.__remove_ax
10101010
self.stale = True
10111011
a.stale_callback = _stale_figure_callback
10121012
return a
10131013

1014+
def __remove_ax(self, ax):
1015+
def _reset_loc_form(axis):
1016+
axis.set_major_formatter(axis.get_major_formatter())
1017+
axis.set_major_locator(axis.get_major_locator())
1018+
axis.set_minor_formatter(axis.get_minor_formatter())
1019+
axis.set_minor_locator(axis.get_minor_locator())
1020+
1021+
def _break_share_link(ax, grouper):
1022+
siblings = grouper.get_siblings(ax)
1023+
if len(siblings) > 1:
1024+
grouper.remove(ax)
1025+
for last_ax in siblings:
1026+
if ax is last_ax:
1027+
continue
1028+
return last_ax
1029+
return None
1030+
1031+
self.delaxes(ax)
1032+
last_ax = _break_share_link(ax, ax._shared_y_axes)
1033+
if last_ax is not None:
1034+
_reset_loc_form(last_ax.yaxis)
1035+
1036+
last_ax = _break_share_link(ax, ax._shared_x_axes)
1037+
if last_ax is not None:
1038+
_reset_loc_form(last_ax.xaxis)
1039+
10141040
def clf(self, keep_observers=False):
10151041
"""
10161042
Clear the figure.

0 commit comments

Comments
 (0)