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

Skip to content

Commit 9333b45

Browse files
authored
Merge pull request #26026 from anntzer/da
Simplify delaxes.
2 parents b200f02 + 18bd2e6 commit 9333b45

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

lib/matplotlib/cbook.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ def joined(self, a, b):
827827
return (self._mapping.get(a, object()) is self._mapping.get(b))
828828

829829
def remove(self, a):
830+
"""Remove *a* from the grouper, doing nothing if it is not there."""
830831
set_a = self._mapping.pop(a, None)
831832
if set_a:
832833
set_a.remove(a)

lib/matplotlib/figure.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -925,38 +925,28 @@ def delaxes(self, ax):
925925
Remove the `~.axes.Axes` *ax* from the figure; update the current Axes.
926926
"""
927927

928-
def _reset_locators_and_formatters(axis):
929-
# Set the formatters and locators to be associated with axis
930-
# (where previously they may have been associated with another
931-
# Axis instance)
932-
axis.get_major_formatter().set_axis(axis)
933-
axis.get_major_locator().set_axis(axis)
934-
axis.get_minor_formatter().set_axis(axis)
935-
axis.get_minor_locator().set_axis(axis)
936-
937-
def _break_share_link(ax, grouper):
938-
siblings = grouper.get_siblings(ax)
939-
if len(siblings) > 1:
940-
grouper.remove(ax)
941-
for last_ax in siblings:
942-
if ax is not last_ax:
943-
return last_ax
944-
return None
945-
946928
self._axstack.remove(ax)
947929
self._axobservers.process("_axes_change_event", self)
948930
self.stale = True
949931
self._localaxes.remove(ax)
950932
self.canvas.release_mouse(ax)
951933

952-
# Break link between any shared axes
953-
for name in ax._axis_names:
954-
last_ax = _break_share_link(ax, ax._shared_axes[name])
955-
if last_ax is not None:
956-
_reset_locators_and_formatters(last_ax._axis_map[name])
957-
958-
# Break link between any twinned axes
959-
_break_share_link(ax, ax._twinned_axes)
934+
for name in ax._axis_names: # Break link between any shared axes
935+
grouper = ax._shared_axes[name]
936+
siblings = [other for other in grouper.get_siblings(ax) if other is not ax]
937+
if not siblings: # Axes was not shared along this axis; we're done.
938+
continue
939+
grouper.remove(ax)
940+
# Formatters and locators may previously have been associated with the now
941+
# removed axis. Update them to point to an axis still there (we can pick
942+
# any of them, and use the first sibling).
943+
remaining_axis = siblings[0]._axis_map[name]
944+
remaining_axis.get_major_formatter().set_axis(remaining_axis)
945+
remaining_axis.get_major_locator().set_axis(remaining_axis)
946+
remaining_axis.get_minor_formatter().set_axis(remaining_axis)
947+
remaining_axis.get_minor_locator().set_axis(remaining_axis)
948+
949+
ax._twinned_axes.remove(ax) # Break link between any twinned axes.
960950

961951
def clear(self, keep_observers=False):
962952
"""

0 commit comments

Comments
 (0)