Skip to content

Commit 24fb288

Browse files
committed
Dedupe implementations of {XAxis,YAxis}._get_tick_boxes_siblings.
A single implementation can be written in the base class. Also, the alignment groupers only need to be defined in FigureBase; we don't need to redefine them in the Figure subclass.
1 parent 56d9e7c commit 24fb288

File tree

2 files changed

+20
-51
lines changed

2 files changed

+20
-51
lines changed

lib/matplotlib/axis.py

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,14 +1833,28 @@ def set_ticks(self, ticks, *, minor=False):
18331833
self.set_major_locator(mticker.FixedLocator(ticks))
18341834
return self.get_major_ticks(len(ticks))
18351835

1836-
def _get_tick_boxes_siblings(self, xdir, renderer):
1836+
def _get_tick_boxes_siblings(self, renderer):
18371837
"""
18381838
Get the bounding boxes for this `.axis` and its siblings
1839-
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
1839+
as set by `.Figure.align_xlabels` or `.Figure.align_ylabels`.
18401840
18411841
By default it just gets bboxes for self.
18421842
"""
1843-
raise NotImplementedError('Derived must override')
1843+
bboxes = []
1844+
bboxes2 = []
1845+
# Get the Grouper keeping track of x or y label groups for this figure.
1846+
axis_name, = [
1847+
name for name, axis in self.axes._get_axis_map().items()
1848+
if name in self.figure._align_label_groups and axis is self]
1849+
grouper = self.figure._align_label_groups[axis_name]
1850+
# If we want to align labels from other axes:
1851+
for ax in grouper.get_siblings(self.axes):
1852+
axis = ax._get_axis_map()[axis_name]
1853+
ticks_to_draw = axis._update_ticks()
1854+
tlb, tlb2 = axis._get_tick_bboxes(ticks_to_draw, renderer)
1855+
bboxes.extend(tlb)
1856+
bboxes2.extend(tlb2)
1857+
return bboxes, bboxes2
18441858

18451859
def _update_label_position(self, renderer):
18461860
"""
@@ -2039,25 +2053,6 @@ def set_label_position(self, position):
20392053
self.label_position = position
20402054
self.stale = True
20412055

2042-
def _get_tick_boxes_siblings(self, renderer):
2043-
"""
2044-
Get the bounding boxes for this `.axis` and its siblings
2045-
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
2046-
2047-
By default it just gets bboxes for self.
2048-
"""
2049-
bboxes = []
2050-
bboxes2 = []
2051-
# get the Grouper that keeps track of x-label groups for this figure
2052-
grp = self.figure._align_xlabel_grp
2053-
# if we want to align labels from other axes:
2054-
for nn, axx in enumerate(grp.get_siblings(self.axes)):
2055-
ticks_to_draw = axx.xaxis._update_ticks()
2056-
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
2057-
bboxes.extend(tlb)
2058-
bboxes2.extend(tlb2)
2059-
return bboxes, bboxes2
2060-
20612056
def _update_label_position(self, renderer):
20622057
"""
20632058
Update the label position based on the bounding box enclosing
@@ -2330,25 +2325,6 @@ def set_label_position(self, position):
23302325
self.label_position = position
23312326
self.stale = True
23322327

2333-
def _get_tick_boxes_siblings(self, renderer):
2334-
"""
2335-
Get the bounding boxes for this `.axis` and its siblings
2336-
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
2337-
2338-
By default it just gets bboxes for self.
2339-
"""
2340-
bboxes = []
2341-
bboxes2 = []
2342-
# get the Grouper that keeps track of y-label groups for this figure
2343-
grp = self.figure._align_ylabel_grp
2344-
# if we want to align labels from other axes:
2345-
for axx in grp.get_siblings(self.axes):
2346-
ticks_to_draw = axx.yaxis._update_ticks()
2347-
tlb, tlb2 = axx.yaxis._get_tick_bboxes(ticks_to_draw, renderer)
2348-
bboxes.extend(tlb)
2349-
bboxes2.extend(tlb2)
2350-
return bboxes, bboxes2
2351-
23522328
def _update_label_position(self, renderer):
23532329
"""
23542330
Update the label position based on the bounding box enclosing

lib/matplotlib/figure.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ def __init__(self):
136136
# groupers to keep track of x and y labels we want to align.
137137
# see self.align_xlabels and self.align_ylabels and
138138
# axis._get_tick_boxes_siblings
139-
self._align_xlabel_grp = cbook.Grouper()
140-
self._align_ylabel_grp = cbook.Grouper()
139+
self._align_label_groups = {"x": cbook.Grouper(), "y": cbook.Grouper()}
141140

142141
self.figure = self
143142
# list of child gridspecs for this figure
@@ -1203,7 +1202,7 @@ def align_xlabels(self, axs=None):
12031202
if (pos == 'top' and rowspan.start == rowspanc.start or
12041203
pos == 'bottom' and rowspan.stop == rowspanc.stop):
12051204
# grouper for groups of xlabels to align
1206-
self._align_xlabel_grp.join(ax, axc)
1205+
self._align_label_groups['x'].join(ax, axc)
12071206

12081207
def align_ylabels(self, axs=None):
12091208
"""
@@ -1263,7 +1262,7 @@ def align_ylabels(self, axs=None):
12631262
if (pos == 'left' and colspan.start == colspanc.start or
12641263
pos == 'right' and colspan.stop == colspanc.stop):
12651264
# grouper for groups of ylabels to align
1266-
self._align_ylabel_grp.join(ax, axc)
1265+
self._align_label_groups['y'].join(ax, axc)
12671266

12681267
def align_labels(self, axs=None):
12691268
"""
@@ -2155,12 +2154,6 @@ def __init__(self,
21552154

21562155
self.set_constrained_layout(constrained_layout)
21572156

2158-
# groupers to keep track of x and y labels we want to align.
2159-
# see self.align_xlabels and self.align_ylabels and
2160-
# axis._get_tick_boxes_siblings
2161-
self._align_xlabel_grp = cbook.Grouper()
2162-
self._align_ylabel_grp = cbook.Grouper()
2163-
21642157
# list of child gridspecs for this figure
21652158
self._gridspecs = []
21662159

0 commit comments

Comments
 (0)