Skip to content

Commit f4e4f26

Browse files
committed
Allow sharing Locators and Formatters across Axises.
1 parent 327df3b commit f4e4f26

File tree

2 files changed

+75
-54
lines changed

2 files changed

+75
-54
lines changed

lib/matplotlib/axis.py

+73-4
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,20 @@ class Ticker:
643643
"""
644644

645645
def __init__(self):
646+
self._axis = None
646647
self._locator = None
647648
self._formatter = None
649+
self._locator_is_default = True
650+
self._formatter_is_default = True
651+
652+
# The machinery below allows TickHelpers to be shared over multiple Axis.
648653

649654
@property
650655
def locator(self):
651-
return self._locator
656+
locator = self._locator
657+
if locator is not None:
658+
locator.set_axis(self._axis)
659+
return locator
652660

653661
@locator.setter
654662
def locator(self, locator):
@@ -661,7 +669,10 @@ def locator(self, locator):
661669

662670
@property
663671
def formatter(self):
664-
return self._formatter
672+
formatter = self._formatter
673+
if formatter is not None:
674+
formatter.set_axis(self._axis)
675+
return formatter
665676

666677
@formatter.setter
667678
def formatter(self, formatter):
@@ -672,6 +683,10 @@ def formatter(self, formatter):
672683
"and support for them will be removed %(removal)s.")
673684
self._formatter = formatter
674685

686+
@locator.setter
687+
def locator(self, locator):
688+
self._locator = locator
689+
675690

676691
class _LazyTickList:
677692
"""
@@ -759,8 +774,8 @@ def __init__(self, axes, pickradius=15):
759774
self.isDefault_label = True
760775

761776
self.axes = axes
762-
self.major = Ticker()
763-
self.minor = Ticker()
777+
self._major = Ticker()
778+
self._minor = Ticker()
764779
self.callbacks = cbook.CallbackRegistry()
765780

766781
self._autolabelpos = True
@@ -779,6 +794,60 @@ def __init__(self, axes, pickradius=15):
779794
self.cla()
780795
self._set_scale('linear')
781796

797+
# The machinery below allows TickHelpers to be shared over multiple Axis.
798+
799+
@property
800+
def major(self):
801+
major = self._major
802+
major._axis = self
803+
return major
804+
805+
@major.setter
806+
def major(self, major):
807+
self._major = major
808+
809+
@property
810+
def minor(self):
811+
minor = self._minor
812+
minor._axis = self
813+
return minor
814+
815+
@minor.setter
816+
def minor(self, minor):
817+
self._minor = minor
818+
819+
@property
820+
def isDefault_majloc(self):
821+
return self.major._locator_is_default
822+
823+
@isDefault_majloc.setter
824+
def isDefault_majloc(self, value):
825+
self.major._locator_is_default = value
826+
827+
@property
828+
def isDefault_majfmt(self):
829+
return self.major._formatter_is_default
830+
831+
@isDefault_majfmt.setter
832+
def isDefault_majfmt(self, value):
833+
self.major._formatter_is_default = value
834+
835+
@property
836+
def isDefault_minloc(self):
837+
return self.minor._locator_is_default
838+
839+
@isDefault_minloc.setter
840+
def isDefault_minloc(self, value):
841+
self.minor._locator_is_default = value
842+
843+
@property
844+
def isDefault_minfmt(self):
845+
return self.minor._formatter_is_default
846+
847+
@isDefault_minfmt.setter
848+
def isDefault_minfmt(self, value):
849+
self.minor._formatter_is_default = value
850+
782851
# During initialization, Axis objects often create ticks that are later
783852
# unused; this turns out to be a very slow step. Instead, use a custom
784853
# descriptor to make the tick lists lazy and instantiate them as needed.

lib/matplotlib/figure.py

+2-50
Original file line numberDiff line numberDiff line change
@@ -1557,60 +1557,12 @@ def delaxes(self, ax):
15571557
"""
15581558
Remove the `~.axes.Axes` *ax* from the figure; update the current axes.
15591559
"""
1560-
1561-
def _reset_locators_and_formatters(axis):
1562-
# Set the formatters and locators to be associated with axis
1563-
# (where previously they may have been associated with another
1564-
# Axis isntance)
1565-
#
1566-
# Because set_major_formatter() etc. force isDefault_* to be False,
1567-
# we have to manually check if the original formatter was a
1568-
# default and manually set isDefault_* if that was the case.
1569-
majfmt = axis.get_major_formatter()
1570-
isDefault = majfmt.axis.isDefault_majfmt
1571-
axis.set_major_formatter(majfmt)
1572-
if isDefault:
1573-
majfmt.axis.isDefault_majfmt = True
1574-
1575-
majloc = axis.get_major_locator()
1576-
isDefault = majloc.axis.isDefault_majloc
1577-
axis.set_major_locator(majloc)
1578-
if isDefault:
1579-
majloc.axis.isDefault_majloc = True
1580-
1581-
minfmt = axis.get_minor_formatter()
1582-
isDefault = majloc.axis.isDefault_minfmt
1583-
axis.set_minor_formatter(minfmt)
1584-
if isDefault:
1585-
minfmt.axis.isDefault_minfmt = True
1586-
1587-
minloc = axis.get_minor_locator()
1588-
isDefault = majloc.axis.isDefault_minloc
1589-
axis.set_minor_locator(minloc)
1590-
if isDefault:
1591-
minloc.axis.isDefault_minloc = True
1592-
1593-
def _break_share_link(ax, grouper):
1594-
siblings = grouper.get_siblings(ax)
1595-
if len(siblings) > 1:
1596-
grouper.remove(ax)
1597-
for last_ax in siblings:
1598-
if ax is not last_ax:
1599-
return last_ax
1600-
return None
1601-
16021560
self._axstack.remove(ax)
16031561
for func in self._axobservers:
16041562
func(self)
16051563
self.stale = True
1606-
1607-
last_ax = _break_share_link(ax, ax._shared_y_axes)
1608-
if last_ax is not None:
1609-
_reset_locators_and_formatters(last_ax.yaxis)
1610-
1611-
last_ax = _break_share_link(ax, ax._shared_x_axes)
1612-
if last_ax is not None:
1613-
_reset_locators_and_formatters(last_ax.xaxis)
1564+
ax._shared_x_axes.remove(ax)
1565+
ax._shared_y_axes.remove(ax)
16141566

16151567
def clf(self, keep_observers=False):
16161568
"""

0 commit comments

Comments
 (0)