From 1f0cd1655f305f9a11fb97e1c1eca3eabdb8ca6f Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 11 Feb 2021 14:55:54 +0100 Subject: [PATCH] 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. --- lib/matplotlib/axis.py | 61 +++++++++++++--------------------------- lib/matplotlib/figure.py | 13 ++------- 2 files changed, 23 insertions(+), 51 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 7ec16d04d2a2..c28d185057b8 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -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): """ @@ -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 @@ -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 diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index f4e5183e3838..85dfe2ca5576 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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 @@ -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): """ @@ -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): """ @@ -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 = []