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

Skip to content

Commit a142ed0

Browse files
committed
Allow sharing Locators and Formatters across Axises.
1 parent b09aad2 commit a142ed0

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

lib/matplotlib/axis.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,20 @@ class Ticker:
539539
"""
540540

541541
def __init__(self):
542+
self._axis = None
542543
self._locator = None
543544
self._formatter = None
544545
self._locator_is_default = True
545546
self._formatter_is_default = True
546547

548+
# The machinery below allows TickHelpers to be shared over multiple Axis.
549+
547550
@property
548551
def locator(self):
549-
return self._locator
552+
locator = self._locator
553+
if locator is not None:
554+
locator.set_axis(self._axis)
555+
return locator
550556

551557
@locator.setter
552558
def locator(self, locator):
@@ -557,7 +563,10 @@ def locator(self, locator):
557563

558564
@property
559565
def formatter(self):
560-
return self._formatter
566+
formatter = self._formatter
567+
if formatter is not None:
568+
formatter.set_axis(self._axis)
569+
return formatter
561570

562571
@formatter.setter
563572
def formatter(self, formatter):
@@ -566,6 +575,10 @@ def formatter(self, formatter):
566575
'matplotlib.ticker.Formatter')
567576
self._formatter = formatter
568577

578+
@locator.setter
579+
def locator(self, locator):
580+
self._locator = locator
581+
569582

570583
class _LazyTickList:
571584
"""
@@ -653,8 +666,8 @@ def __init__(self, axes, pickradius=15):
653666
self.isDefault_label = True
654667

655668
self.axes = axes
656-
self.major = Ticker()
657-
self.minor = Ticker()
669+
self._major = Ticker()
670+
self._minor = Ticker()
658671
self.callbacks = cbook.CallbackRegistry()
659672

660673
self._autolabelpos = True
@@ -680,6 +693,28 @@ def __init__(self, axes, pickradius=15):
680693
self.clear()
681694
self._set_scale('linear')
682695

696+
# The machinery below allows TickHelpers to be shared over multiple Axis.
697+
698+
@property
699+
def major(self):
700+
major = self._major
701+
major._axis = self
702+
return major
703+
704+
@major.setter
705+
def major(self, major):
706+
self._major = major
707+
708+
@property
709+
def minor(self):
710+
minor = self._minor
711+
minor._axis = self
712+
return minor
713+
714+
@minor.setter
715+
def minor(self, minor):
716+
self._minor = minor
717+
683718
@property
684719
def isDefault_majloc(self):
685720
return self.major._locator_is_default

lib/matplotlib/figure.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -902,38 +902,12 @@ def delaxes(self, ax):
902902
"""
903903
Remove the `~.axes.Axes` *ax* from the figure; update the current Axes.
904904
"""
905-
906-
def _reset_locators_and_formatters(axis):
907-
# Set the formatters and locators to be associated with axis
908-
# (where previously they may have been associated with another
909-
# Axis instance)
910-
axis.get_major_formatter().set_axis(axis)
911-
axis.get_major_locator().set_axis(axis)
912-
axis.get_minor_formatter().set_axis(axis)
913-
axis.get_minor_locator().set_axis(axis)
914-
915-
def _break_share_link(ax, grouper):
916-
siblings = grouper.get_siblings(ax)
917-
if len(siblings) > 1:
918-
grouper.remove(ax)
919-
for last_ax in siblings:
920-
if ax is not last_ax:
921-
return last_ax
922-
return None
923-
924905
self._axstack.remove(ax)
925906
self._axobservers.process("_axes_change_event", self)
926907
self.stale = True
927908
self._localaxes.remove(ax)
928-
929-
# Break link between any shared axes
930-
for name in ax._axis_names:
931-
last_ax = _break_share_link(ax, ax._shared_axes[name])
932-
if last_ax is not None:
933-
_reset_locators_and_formatters(getattr(last_ax, f"{name}axis"))
934-
935-
# Break link between any twinned axes
936-
_break_share_link(ax, ax._twinned_axes)
909+
for axis_name in ax._shared_axes:
910+
ax._shared_axes[axis_name].remove(ax)
937911

938912
# Note: in the docstring below, the newlines in the examples after the
939913
# calls to legend() allow replacing it with figlegend() to generate the

0 commit comments

Comments
 (0)