Skip to content

Commit 71a8998

Browse files
authored
Merge pull request matplotlib#24983 from oscargus/axisrefactor
Refactor parts of Axis for readability
2 parents 0b131ee + 12a7f76 commit 71a8998

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

lib/matplotlib/axis.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,21 @@ def isDefault_minfmt(self):
718718
def isDefault_minfmt(self, value):
719719
self.minor._formatter_is_default = value
720720

721+
def _get_shared_axes(self):
722+
"""Return Grouper of shared axes for current axis."""
723+
return self.axes._shared_axes[
724+
self._get_axis_name()].get_siblings(self.axes)
725+
726+
def _get_shared_axis(self):
727+
"""Return list of shared axis for current axis."""
728+
name = self._get_axis_name()
729+
return [ax._axis_map[name] for ax in self._get_shared_axes()]
730+
731+
def _get_axis_name(self):
732+
"""Return the axis name."""
733+
return [name for name, axis in self.axes._axis_map.items()
734+
if axis is self][0]
735+
721736
# During initialization, Axis objects often create ticks that are later
722737
# unused; this turns out to be a very slow step. Instead, use a custom
723738
# descriptor to make the tick lists lazy and instantiate them as needed.
@@ -801,12 +816,10 @@ def _set_axes_scale(self, value, **kwargs):
801816
`matplotlib.scale.register_scale`. These scales can then also
802817
be used here.
803818
"""
804-
name, = [name for name, axis in self.axes._axis_map.items()
805-
if axis is self] # The axis name.
819+
name = self._get_axis_name()
806820
old_default_lims = (self.get_major_locator()
807821
.nonsingular(-np.inf, np.inf))
808-
g = self.axes._shared_axes[name]
809-
for ax in g.get_siblings(self.axes):
822+
for ax in self._get_shared_axes():
810823
ax._axis_map[name]._set_scale(value, **kwargs)
811824
ax._update_transScale()
812825
ax.stale = True
@@ -1172,8 +1185,7 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
11721185
Whether to turn on autoscaling of the x-axis. True turns on, False
11731186
turns off, None leaves unchanged.
11741187
"""
1175-
name, = [name for name, axis in self.axes._axis_map.items()
1176-
if axis is self] # The axis name.
1188+
name = self._get_axis_name()
11771189

11781190
self.axes._process_unit_info([(name, (v0, v1))], convert=False)
11791191
v0 = self.axes._validate_converted_limits(v0, self.convert_units)
@@ -1211,15 +1223,15 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
12111223

12121224
self.set_view_interval(v0, v1, ignore=True)
12131225
# Mark viewlims as no longer stale without triggering an autoscale.
1214-
for ax in self.axes._shared_axes[name].get_siblings(self.axes):
1226+
for ax in self._get_shared_axes():
12151227
ax._stale_viewlims[name] = False
12161228
if auto is not None:
12171229
self._set_autoscale_on(bool(auto))
12181230

12191231
if emit:
12201232
self.axes.callbacks.process(f"{name}lim_changed", self.axes)
12211233
# Call all of the other axes that are shared with this one
1222-
for other in self.axes._shared_axes[name].get_siblings(self.axes):
1234+
for other in self._get_shared_axes():
12231235
if other is not self.axes:
12241236
other._axis_map[name]._set_lim(
12251237
v0, v1, emit=False, auto=auto)
@@ -1753,16 +1765,7 @@ def set_units(self, u):
17531765
"""
17541766
if u == self.units:
17551767
return
1756-
for name, axis in self.axes._axis_map.items():
1757-
if self is axis:
1758-
shared = [
1759-
getattr(ax, f"{name}axis")
1760-
for ax
1761-
in self.axes._shared_axes[name].get_siblings(self.axes)]
1762-
break
1763-
else:
1764-
shared = [self]
1765-
for axis in shared:
1768+
for axis in self._get_shared_axis():
17661769
axis.units = u
17671770
axis._update_axisinfo()
17681771
axis.callbacks.process('units')
@@ -2017,17 +2020,8 @@ def _set_tick_locations(self, ticks, *, minor=False):
20172020
# XXX if the user changes units, the information will be lost here
20182021
ticks = self.convert_units(ticks)
20192022
locator = mticker.FixedLocator(ticks) # validate ticks early.
2020-
for name, axis in self.axes._axis_map.items():
2021-
if self is axis:
2022-
shared = [
2023-
getattr(ax, f"{name}axis")
2024-
for ax
2025-
in self.axes._shared_axes[name].get_siblings(self.axes)]
2026-
break
2027-
else:
2028-
shared = [self]
20292023
if len(ticks):
2030-
for axis in shared:
2024+
for axis in self._get_shared_axis():
20312025
# set_view_interval maintains any preexisting inversion.
20322026
axis.set_view_interval(min(ticks), max(ticks))
20332027
self.axes.stale = True
@@ -2088,18 +2082,15 @@ def _get_tick_boxes_siblings(self, renderer):
20882082
By default, it just gets bboxes for *self*.
20892083
"""
20902084
# Get the Grouper keeping track of x or y label groups for this figure.
2091-
axis_names = [
2092-
name for name, axis in self.axes._axis_map.items()
2093-
if name in self.figure._align_label_groups and axis is self]
2094-
if len(axis_names) != 1:
2085+
name = self._get_axis_name()
2086+
if name not in self.figure._align_label_groups:
20952087
return [], []
2096-
axis_name, = axis_names
2097-
grouper = self.figure._align_label_groups[axis_name]
2088+
grouper = self.figure._align_label_groups[name]
20982089
bboxes = []
20992090
bboxes2 = []
21002091
# If we want to align labels from other Axes:
21012092
for ax in grouper.get_siblings(self.axes):
2102-
axis = getattr(ax, f"{axis_name}axis")
2093+
axis = ax._axis_map[name]
21032094
ticks_to_draw = axis._update_ticks()
21042095
tlb, tlb2 = axis._get_ticklabel_bboxes(ticks_to_draw, renderer)
21052096
bboxes.extend(tlb)

0 commit comments

Comments
 (0)