Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17d1025

Browse files
committed
Changed from lists to Grouper
1 parent a8f72f3 commit 17d1025

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

lib/matplotlib/axis.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,6 @@ def __init__(self, axes, pickradius=15):
692692

693693
self._autolabelpos = True
694694
self._smart_bounds = False
695-
self._align_label_siblings = [self]
696695

697696
self.label = self._get_label()
698697
self.labelpad = rcParams['axes.labelpad']
@@ -1673,22 +1672,14 @@ def set_ticks(self, ticks, minor=False):
16731672
self.set_major_locator(mticker.FixedLocator(ticks))
16741673
return self.get_major_ticks(len(ticks))
16751674

1676-
def _get_tick_boxes_siblings(self, renderer):
1675+
def _get_tick_boxes_siblings(self, xdir, renderer):
16771676
"""
16781677
Get the bounding boxes for this `.axis` and its siblings
16791678
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
16801679
16811680
By default it just gets bboxes for self.
16821681
"""
1683-
bboxes = []
1684-
bboxes2 = []
1685-
# if we want to align labels from other axes:
1686-
for axx in self._align_label_siblings:
1687-
ticks_to_draw = axx._update_ticks(renderer)
1688-
tlb, tlb2 = axx._get_tick_bboxes(ticks_to_draw, renderer)
1689-
bboxes.extend(tlb)
1690-
bboxes2.extend(tlb2)
1691-
return bboxes, bboxes2
1682+
raise NotImplementedError('Derived must override')
16921683

16931684
def _update_label_position(self, renderer):
16941685
"""
@@ -1866,6 +1857,24 @@ def set_label_position(self, position):
18661857
self.label_position = position
18671858
self.stale = True
18681859

1860+
def _get_tick_boxes_siblings(self, renderer):
1861+
"""
1862+
Get the bounding boxes for this `.axis` and its siblings
1863+
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
1864+
1865+
By default it just gets bboxes for self.
1866+
"""
1867+
bboxes = []
1868+
bboxes2 = []
1869+
grp = self.figure._align_xlabel_grp
1870+
# if we want to align labels from other axes:
1871+
for axx in grp.get_siblings(self.axes):
1872+
ticks_to_draw = axx.xaxis._update_ticks(renderer)
1873+
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
1874+
bboxes.extend(tlb)
1875+
bboxes2.extend(tlb2)
1876+
return bboxes, bboxes2
1877+
18691878
def _update_label_position(self, renderer):
18701879
"""
18711880
Update the label position based on the bounding box enclosing
@@ -1876,7 +1885,7 @@ def _update_label_position(self, renderer):
18761885

18771886
# get bounding boxes for this axis and any siblings
18781887
# that have been set by `fig.align_xlabels()`
1879-
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer)
1888+
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
18801889

18811890
x, y = self.label.get_position()
18821891
if self.label_position == 'bottom':
@@ -2216,6 +2225,24 @@ def set_label_position(self, position):
22162225
self.label_position = position
22172226
self.stale = True
22182227

2228+
def _get_tick_boxes_siblings(self, renderer):
2229+
"""
2230+
Get the bounding boxes for this `.axis` and its siblings
2231+
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
2232+
2233+
By default it just gets bboxes for self.
2234+
"""
2235+
bboxes = []
2236+
bboxes2 = []
2237+
grp = self.figure._align_ylabel_grp
2238+
# if we want to align labels from other axes:
2239+
for axx in grp.get_siblings(self.axes):
2240+
ticks_to_draw = axx.yaxis._update_ticks(renderer)
2241+
tlb, tlb2 = axx.yaxis._get_tick_bboxes(ticks_to_draw, renderer)
2242+
bboxes.extend(tlb)
2243+
bboxes2.extend(tlb2)
2244+
return bboxes, bboxes2
2245+
22192246
def _update_label_position(self, renderer):
22202247
"""
22212248
Update the label position based on the bounding box enclosing
@@ -2226,7 +2253,7 @@ def _update_label_position(self, renderer):
22262253

22272254
# get bounding boxes for this axis and any siblings
22282255
# that have been set by `fig.align_ylabels()`
2229-
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer)
2256+
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
22302257

22312258
x, y = self.label.get_position()
22322259
if self.label_position == 'left':

lib/matplotlib/figure.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ def __init__(self,
380380
self.clf()
381381
self._cachedRenderer = None
382382

383+
# groupers to keep track of x and y labels we want to align.
384+
self._align_xlabel_grp = cbook.Grouper()
385+
self._align_ylabel_grp = cbook.Grouper()
386+
383387
@property
384388
@cbook.deprecated("2.1", alternative="Figure.patch")
385389
def figurePatch(self):
@@ -2103,6 +2107,11 @@ def align_xlabels(self, axs=None):
21032107
Optional list of (or ndarray) `~matplotlib.axes.Axes` to align
21042108
the xlabels. Default is to align all axes on the figure.
21052109
2110+
Note
2111+
----
2112+
This assumes that ``axs`` are from the same `~.GridSpec`, so that
2113+
their `~.SubplotSpec` positions correspond to figure positions.
2114+
21062115
See Also
21072116
--------
21082117
matplotlib.figure.Figure.align_ylabels
@@ -2124,7 +2133,6 @@ def align_xlabels(self, axs=None):
21242133

21252134
if axs is None:
21262135
axs = self.axes
2127-
21282136
axs = np.asarray(np.array(axs)).flatten().tolist()
21292137

21302138
for ax in axs:
@@ -2136,7 +2144,7 @@ def align_xlabels(self, axs=None):
21362144
# loop through other axes, and search for label positions
21372145
# that are same as this one, and that share the appropriate
21382146
# row number.
2139-
# Add to a list associated with each axes of sibblings.
2147+
# Add to a grouper associated with each axes of sibblings.
21402148
# This list is inspected in `axis.draw` by
21412149
# `axis._update_label_position`.
21422150
for axc in axs:
@@ -2146,7 +2154,8 @@ def align_xlabels(self, axs=None):
21462154
ss.get_rows_columns()
21472155
if (labpo == 'bottom' and rowc1 == row1 or
21482156
labpo == 'top' and rowc0 == row0):
2149-
axc.xaxis._align_label_siblings += [ax.xaxis]
2157+
# grouper for groups of xlabels to align
2158+
self._align_xlabel_grp.join(ax, axc)
21502159

21512160
def align_ylabels(self, axs=None):
21522161
"""
@@ -2167,6 +2176,11 @@ def align_ylabels(self, axs=None):
21672176
Optional list (or ndarray) of `~matplotlib.axes.Axes` to align
21682177
the ylabels. Default is to align all axes on the figure.
21692178
2179+
Note
2180+
----
2181+
This assumes that ``axs`` are from the same `~.GridSpec`, so that
2182+
their `~.SubplotSpec` positions correspond to figure positions.
2183+
21702184
See Also
21712185
--------
21722186
matplotlib.figure.Figure.align_xlabels
@@ -2187,7 +2201,6 @@ def align_ylabels(self, axs=None):
21872201

21882202
if axs is None:
21892203
axs = self.axes
2190-
21912204
axs = np.asarray(np.array(axs)).flatten().tolist()
21922205
for ax in axs:
21932206
_log.debug(' Working on: %s', ax.get_ylabel())
@@ -2209,7 +2222,8 @@ def align_ylabels(self, axs=None):
22092222
ss.get_rows_columns()
22102223
if (labpo == 'left' and colc0 == col0 or
22112224
labpo == 'right' and colc1 == col1):
2212-
axc.yaxis._align_label_siblings += [ax.yaxis]
2225+
# grouper for groups of ylabels to align
2226+
self._align_ylabel_grp.join(ax, axc)
22132227

22142228
def align_labels(self, axs=None):
22152229
"""

0 commit comments

Comments
 (0)