Skip to content

Allow sharing Locators and Formatters across Axises. #13482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,20 @@ class Ticker:
"""

def __init__(self):
self._axis = None
self._locator = None
self._formatter = None
self._locator_is_default = True
self._formatter_is_default = True

# The machinery below allows TickHelpers to be shared over multiple Axis.

@property
def locator(self):
return self._locator
locator = self._locator
if locator is not None:
locator.set_axis(self._axis)
return locator

@locator.setter
def locator(self, locator):
Expand All @@ -557,7 +563,10 @@ def locator(self, locator):

@property
def formatter(self):
return self._formatter
formatter = self._formatter
if formatter is not None:
formatter.set_axis(self._axis)
return formatter

@formatter.setter
def formatter(self, formatter):
Expand All @@ -566,6 +575,10 @@ def formatter(self, formatter):
'matplotlib.ticker.Formatter')
self._formatter = formatter

@locator.setter
def locator(self, locator):
self._locator = locator


class _LazyTickList:
"""
Expand Down Expand Up @@ -653,8 +666,8 @@ def __init__(self, axes, pickradius=15):
self.isDefault_label = True

self.axes = axes
self.major = Ticker()
self.minor = Ticker()
self._major = Ticker()
self._minor = Ticker()
self.callbacks = cbook.CallbackRegistry()

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

# The machinery below allows TickHelpers to be shared over multiple Axis.

@property
def major(self):
major = self._major
major._axis = self
return major

@major.setter
def major(self, major):
self._major = major

@property
def minor(self):
minor = self._minor
minor._axis = self
return minor

@minor.setter
def minor(self, minor):
self._minor = minor

@property
def isDefault_majloc(self):
return self.major._locator_is_default
Expand Down
30 changes: 2 additions & 28 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,38 +902,12 @@ def delaxes(self, ax):
"""
Remove the `~.axes.Axes` *ax* from the figure; update the current Axes.
"""

def _reset_locators_and_formatters(axis):
# Set the formatters and locators to be associated with axis
# (where previously they may have been associated with another
# Axis instance)
axis.get_major_formatter().set_axis(axis)
axis.get_major_locator().set_axis(axis)
axis.get_minor_formatter().set_axis(axis)
axis.get_minor_locator().set_axis(axis)

def _break_share_link(ax, grouper):
siblings = grouper.get_siblings(ax)
if len(siblings) > 1:
grouper.remove(ax)
for last_ax in siblings:
if ax is not last_ax:
return last_ax
return None

self._axstack.remove(ax)
self._axobservers.process("_axes_change_event", self)
self.stale = True
self._localaxes.remove(ax)

# Break link between any shared axes
for name in ax._axis_names:
last_ax = _break_share_link(ax, ax._shared_axes[name])
if last_ax is not None:
_reset_locators_and_formatters(getattr(last_ax, f"{name}axis"))

# Break link between any twinned axes
_break_share_link(ax, ax._twinned_axes)
for axis_name in ax._shared_axes:
ax._shared_axes[axis_name].remove(ax)

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