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

Skip to content

Commit 1a23fd3

Browse files
committed
Allow sharing Locators and Formatters across Axises.
1 parent 9856ec2 commit 1a23fd3

File tree

2 files changed

+75
-52
lines changed

2 files changed

+75
-52
lines changed

lib/matplotlib/axis.py

Lines changed: 73 additions & 4 deletions
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

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,55 +1602,9 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
16021602
return axarr
16031603

16041604
def _remove_ax(self, ax):
1605-
def _reset_locators_and_formatters(axis):
1606-
# Set the formatters and locators to be associated with axis
1607-
# (where previously they may have been associated with another
1608-
# Axis isntance)
1609-
#
1610-
# Because set_major_formatter() etc. force isDefault_* to be False,
1611-
# we have to manually check if the original formatter was a
1612-
# default and manually set isDefault_* if that was the case.
1613-
majfmt = axis.get_major_formatter()
1614-
isDefault = majfmt.axis.isDefault_majfmt
1615-
axis.set_major_formatter(majfmt)
1616-
if isDefault:
1617-
majfmt.axis.isDefault_majfmt = True
1618-
1619-
majloc = axis.get_major_locator()
1620-
isDefault = majloc.axis.isDefault_majloc
1621-
axis.set_major_locator(majloc)
1622-
if isDefault:
1623-
majloc.axis.isDefault_majloc = True
1624-
1625-
minfmt = axis.get_minor_formatter()
1626-
isDefault = majloc.axis.isDefault_minfmt
1627-
axis.set_minor_formatter(minfmt)
1628-
if isDefault:
1629-
minfmt.axis.isDefault_minfmt = True
1630-
1631-
minloc = axis.get_minor_locator()
1632-
isDefault = majloc.axis.isDefault_minloc
1633-
axis.set_minor_locator(minloc)
1634-
if isDefault:
1635-
minloc.axis.isDefault_minloc = True
1636-
1637-
def _break_share_link(ax, grouper):
1638-
siblings = grouper.get_siblings(ax)
1639-
if len(siblings) > 1:
1640-
grouper.remove(ax)
1641-
for last_ax in siblings:
1642-
if ax is not last_ax:
1643-
return last_ax
1644-
return None
1645-
16461605
self.delaxes(ax)
1647-
last_ax = _break_share_link(ax, ax._shared_y_axes)
1648-
if last_ax is not None:
1649-
_reset_locators_and_formatters(last_ax.yaxis)
1650-
1651-
last_ax = _break_share_link(ax, ax._shared_x_axes)
1652-
if last_ax is not None:
1653-
_reset_locators_and_formatters(last_ax.xaxis)
1606+
ax._shared_x_axes.remove(ax)
1607+
ax._shared_y_axes.remove(ax)
16541608

16551609
def clf(self, keep_observers=False):
16561610
"""

0 commit comments

Comments
 (0)