@@ -662,6 +662,7 @@ def __init__(self, axes, pickradius=15):
662
662
663
663
self ._autolabelpos = True
664
664
self ._smart_bounds = False
665
+ self ._align_label_siblings = [self ]
665
666
666
667
self .label = self ._get_label ()
667
668
self .labelpad = rcParams ['axes.labelpad' ]
@@ -1089,10 +1090,12 @@ def get_tightbbox(self, renderer):
1089
1090
return
1090
1091
1091
1092
ticks_to_draw = self ._update_ticks (renderer )
1092
- ticklabelBoxes , ticklabelBoxes2 = self ._get_tick_bboxes (ticks_to_draw ,
1093
- renderer )
1094
1093
1095
- self ._update_label_position (ticklabelBoxes , ticklabelBoxes2 )
1094
+ self ._update_label_position (renderer )
1095
+
1096
+ # go back to just this axis's tick labels
1097
+ ticklabelBoxes , ticklabelBoxes2 = self ._get_tick_bboxes (
1098
+ ticks_to_draw , renderer )
1096
1099
1097
1100
self ._update_offset_text_position (ticklabelBoxes , ticklabelBoxes2 )
1098
1101
self .offsetText .set_text (self .major .formatter .get_offset ())
@@ -1143,7 +1146,7 @@ def draw(self, renderer, *args, **kwargs):
1143
1146
# *copy* of the axis label box because we don't wan't to scale
1144
1147
# the actual bbox
1145
1148
1146
- self ._update_label_position (ticklabelBoxes , ticklabelBoxes2 )
1149
+ self ._update_label_position (renderer )
1147
1150
1148
1151
self .label .draw (renderer )
1149
1152
@@ -1655,7 +1658,24 @@ def set_ticks(self, ticks, minor=False):
1655
1658
self .set_major_locator (mticker .FixedLocator (ticks ))
1656
1659
return self .get_major_ticks (len (ticks ))
1657
1660
1658
- def _update_label_position (self , bboxes , bboxes2 ):
1661
+ def _get_tick_boxes_siblings (self , renderer ):
1662
+ """
1663
+ Get the bounding boxes for this axis and its sibblings
1664
+ as set by `Figure.align_xlabels` or ``Figure.align_ylables`.
1665
+
1666
+ By default it just gets bboxes for self.
1667
+ """
1668
+ bboxes = []
1669
+ bboxes2 = []
1670
+ # if we want to align labels from other axes:
1671
+ for axx in self ._align_label_siblings :
1672
+ ticks_to_draw = axx ._update_ticks (renderer )
1673
+ tlb , tlb2 = axx ._get_tick_bboxes (ticks_to_draw , renderer )
1674
+ bboxes .extend (tlb )
1675
+ bboxes2 .extend (tlb2 )
1676
+ return bboxes , bboxes2
1677
+
1678
+ def _update_label_position (self , renderer ):
1659
1679
"""
1660
1680
Update the label position based on the bounding box enclosing
1661
1681
all the ticklabels and axis spine
@@ -1832,13 +1852,18 @@ def set_label_position(self, position):
1832
1852
self .label_position = position
1833
1853
self .stale = True
1834
1854
1835
- def _update_label_position (self , bboxes , bboxes2 ):
1855
+ def _update_label_position (self , renderer ):
1836
1856
"""
1837
1857
Update the label position based on the bounding box enclosing
1838
1858
all the ticklabels and axis spine
1839
1859
"""
1840
1860
if not self ._autolabelpos :
1841
1861
return
1862
+
1863
+ # get bounding boxes for this axis and any siblings
1864
+ # that have been set by `fig.align_xlabels()`
1865
+ bboxes , bboxes2 = self ._get_tick_boxes_siblings (renderer )
1866
+
1842
1867
x , y = self .label .get_position ()
1843
1868
if self .label_position == 'bottom' :
1844
1869
try :
@@ -2160,13 +2185,18 @@ def set_label_position(self, position):
2160
2185
self .label_position = position
2161
2186
self .stale = True
2162
2187
2163
- def _update_label_position (self , bboxes , bboxes2 ):
2188
+ def _update_label_position (self , renderer ):
2164
2189
"""
2165
2190
Update the label position based on the bounding box enclosing
2166
2191
all the ticklabels and axis spine
2167
2192
"""
2168
2193
if not self ._autolabelpos :
2169
2194
return
2195
+
2196
+ # get bounding boxes for this axis and any siblings
2197
+ # that have been set by `fig.align_ylabels()`
2198
+ bboxes , bboxes2 = self ._get_tick_boxes_siblings (renderer )
2199
+
2170
2200
x , y = self .label .get_position ()
2171
2201
if self .label_position == 'left' :
2172
2202
try :
@@ -2178,7 +2208,6 @@ def _update_label_position(self, bboxes, bboxes2):
2178
2208
spinebbox = self .axes .bbox
2179
2209
bbox = mtransforms .Bbox .union (bboxes + [spinebbox ])
2180
2210
left = bbox .x0
2181
-
2182
2211
self .label .set_position (
2183
2212
(left - self .labelpad * self .figure .dpi / 72.0 , y )
2184
2213
)
0 commit comments