Skip to content

Dedupe implementations of {XAxis,YAxis}._get_tick_boxes_siblings. #19498

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

Merged
merged 1 commit into from
Feb 17, 2021
Merged
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
61 changes: 20 additions & 41 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,14 +1833,31 @@ def set_ticks(self, ticks, *, minor=False):
self.set_major_locator(mticker.FixedLocator(ticks))
return self.get_major_ticks(len(ticks))

def _get_tick_boxes_siblings(self, xdir, renderer):
def _get_tick_boxes_siblings(self, renderer):
"""
Get the bounding boxes for this `.axis` and its siblings
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
as set by `.Figure.align_xlabels` or `.Figure.align_ylabels`.

By default it just gets bboxes for self.
"""
raise NotImplementedError('Derived must override')
# Get the Grouper keeping track of x or y label groups for this figure.
axis_names = [
name for name, axis in self.axes._get_axis_map().items()
if name in self.figure._align_label_groups and axis is self]
if len(axis_names) != 1:
return [], []
axis_name, = axis_names
grouper = self.figure._align_label_groups[axis_name]
bboxes = []
bboxes2 = []
# If we want to align labels from other axes:
for ax in grouper.get_siblings(self.axes):
axis = ax._get_axis_map()[axis_name]
ticks_to_draw = axis._update_ticks()
tlb, tlb2 = axis._get_tick_bboxes(ticks_to_draw, renderer)
bboxes.extend(tlb)
bboxes2.extend(tlb2)
return bboxes, bboxes2

def _update_label_position(self, renderer):
"""
Expand Down Expand Up @@ -2039,25 +2056,6 @@ def set_label_position(self, position):
self.label_position = position
self.stale = True

def _get_tick_boxes_siblings(self, renderer):
"""
Get the bounding boxes for this `.axis` and its siblings
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.

By default it just gets bboxes for self.
"""
bboxes = []
bboxes2 = []
# get the Grouper that keeps track of x-label groups for this figure
grp = self.figure._align_xlabel_grp
# if we want to align labels from other axes:
for nn, axx in enumerate(grp.get_siblings(self.axes)):
ticks_to_draw = axx.xaxis._update_ticks()
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
bboxes.extend(tlb)
bboxes2.extend(tlb2)
return bboxes, bboxes2

def _update_label_position(self, renderer):
"""
Update the label position based on the bounding box enclosing
Expand Down Expand Up @@ -2330,25 +2328,6 @@ def set_label_position(self, position):
self.label_position = position
self.stale = True

def _get_tick_boxes_siblings(self, renderer):
"""
Get the bounding boxes for this `.axis` and its siblings
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.

By default it just gets bboxes for self.
"""
bboxes = []
bboxes2 = []
# get the Grouper that keeps track of y-label groups for this figure
grp = self.figure._align_ylabel_grp
# if we want to align labels from other axes:
for axx in grp.get_siblings(self.axes):
ticks_to_draw = axx.yaxis._update_ticks()
tlb, tlb2 = axx.yaxis._get_tick_bboxes(ticks_to_draw, renderer)
bboxes.extend(tlb)
bboxes2.extend(tlb2)
return bboxes, bboxes2

def _update_label_position(self, renderer):
"""
Update the label position based on the bounding box enclosing
Expand Down
13 changes: 3 additions & 10 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ def __init__(self):
# groupers to keep track of x and y labels we want to align.
# see self.align_xlabels and self.align_ylabels and
# axis._get_tick_boxes_siblings
self._align_xlabel_grp = cbook.Grouper()
self._align_ylabel_grp = cbook.Grouper()
self._align_label_groups = {"x": cbook.Grouper(), "y": cbook.Grouper()}

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

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

def align_labels(self, axs=None):
"""
Expand Down Expand Up @@ -2155,12 +2154,6 @@ def __init__(self,

self.set_constrained_layout(constrained_layout)

# groupers to keep track of x and y labels we want to align.
# see self.align_xlabels and self.align_ylabels and
# axis._get_tick_boxes_siblings
self._align_xlabel_grp = cbook.Grouper()
self._align_ylabel_grp = cbook.Grouper()

# list of child gridspecs for this figure
self._gridspecs = []

Expand Down