Skip to content

Commit 5d225e9

Browse files
committed
Refinements
1 parent ce91095 commit 5d225e9

File tree

3 files changed

+26
-47
lines changed

3 files changed

+26
-47
lines changed

examples/subplots_axes_and_figures/align_labels_demo.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
Aligning Labels
44
===============
55
6-
Aligning xlabel and ylabel using
7-
`Figure.align_xlabels` and
6+
Aligning xlabel and ylabel using `Figure.align_xlabels` and
87
`Figure.align_ylabels`
98
109
`Figure.align_labels` wraps these two functions.
1110
12-
Note that
13-
the xlabel "XLabel1 1" would normally be much closer to the x-axis, and
14-
"YLabel1 0" would be much closer to the y-axis of their respective axes.
11+
Note that the xlabel "XLabel1 1" would normally be much closer to the
12+
x-axis, and "YLabel1 0" would be much closer to the y-axis of their
13+
respective axes.
1514
"""
1615
import matplotlib.pyplot as plt
1716
import numpy as np
@@ -33,6 +32,6 @@
3332
if i == 0:
3433
for tick in ax.get_xticklabels():
3534
tick.set_rotation(55)
36-
fig.align_labels() # same as fig.align_xlabels() and fig.align_ylabels()
35+
fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels()
3736

3837
plt.show()

lib/matplotlib/axis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1675,8 +1675,8 @@ def set_ticks(self, ticks, minor=False):
16751675

16761676
def _get_tick_boxes_siblings(self, renderer):
16771677
"""
1678-
Get the bounding boxes for this axis and its sibblings
1679-
as set by `Figure.align_xlabels` or ``Figure.align_ylables`.
1678+
Get the bounding boxes for this `.axis` and it's siblings
1679+
as set by `.Figure.align_xlabels` or `.Figure.align_ylablels`.
16801680
16811681
By default it just gets bboxes for self.
16821682
"""

lib/matplotlib/figure.py

+19-39
Original file line numberDiff line numberDiff line change
@@ -2084,7 +2084,7 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None,
20842084
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
20852085
self.subplots_adjust(**kwargs)
20862086

2087-
def align_xlabels(self, axs=None, renderer=None):
2087+
def align_xlabels(self, axs=None):
20882088
"""
20892089
Align the xlabels of subplots in this figure.
20902090
@@ -2099,9 +2099,6 @@ def align_xlabels(self, axs=None, renderer=None):
20992099
Optional list of `~matplotlib.axes.Axes` to align
21002100
the xlabels.
21012101
2102-
renderer : (None)
2103-
Optional renderer to do the adjustment on.
2104-
21052102
See Also
21062103
--------
21072104
matplotlib.figure.Figure.align_ylabels
@@ -2121,11 +2118,6 @@ def align_xlabels(self, axs=None, renderer=None):
21212118
21222119
"""
21232120

2124-
from .tight_layout import get_renderer
2125-
2126-
if renderer is None:
2127-
renderer = get_renderer(self)
2128-
21292121
if axs is None:
21302122
axs = self.axes
21312123

@@ -2135,23 +2127,24 @@ def align_xlabels(self, axs=None, renderer=None):
21352127
_log.debug(' Working on: %s', ax.get_xlabel())
21362128
ss = ax.get_subplotspec()
21372129
nrows, ncols, row0, row1, col0, col1 = ss.get_rows_columns()
2138-
same = [ax]
2139-
labpo = ax.xaxis.get_label_position()
2130+
labpo = ax.xaxis.get_label_position() # top or bottom
2131+
2132+
# loop through other axes, and search for label positions
2133+
# that are same as this one, and that share the appropriate
2134+
# row number.
2135+
# Add to a list associated with each axes of sibblings.
2136+
# This list is inspected in `axis.draw` by
2137+
# `axis._update_label_position`.
21402138
for axc in axs:
21412139
if axc.xaxis.get_label_position() == labpo:
21422140
ss = axc.get_subplotspec()
21432141
nrows, ncols, rowc0, rowc1, colc, col1 = \
21442142
ss.get_rows_columns()
2145-
if (labpo == 'bottom') and (rowc1 == row1):
2146-
same += [axc]
2147-
elif (labpo == 'top') and (rowc0 == row0):
2148-
same += [axc]
2149-
2150-
for axx in same:
2151-
_log.debug(' Same: %s', axx.xaxis.label)
2152-
axx.xaxis._align_label_siblings += [ax.xaxis]
2143+
if (((labpo == 'bottom') and (rowc1 == row1)) or
2144+
((labpo == 'top') and (rowc0 == row0))):
2145+
axc.xaxis._align_label_siblings += [ax.xaxis]
21532146

2154-
def align_ylabels(self, axs=None, renderer=None):
2147+
def align_ylabels(self, axs=None):
21552148
"""
21562149
Align the ylabels of subplots in this figure.
21572150
@@ -2166,9 +2159,6 @@ def align_ylabels(self, axs=None, renderer=None):
21662159
Optional list of `~matplotlib.axes.Axes` to align
21672160
the ylabels.
21682161
2169-
renderer : (None)
2170-
Optional renderer to do the adjustment on.
2171-
21722162
See Also
21732163
--------
21742164
matplotlib.figure.Figure.align_xlabels
@@ -2187,11 +2177,6 @@ def align_ylabels(self, axs=None, renderer=None):
21872177
21882178
"""
21892179

2190-
from .tight_layout import get_renderer
2191-
2192-
if renderer is None:
2193-
renderer = get_renderer(self)
2194-
21952180
if axs is None:
21962181
axs = self.axes
21972182

@@ -2208,13 +2193,9 @@ def align_ylabels(self, axs=None, renderer=None):
22082193
ss = axc.get_subplotspec()
22092194
nrows, ncols, row0, row1, colc0, colc1 = \
22102195
ss.get_rows_columns()
2211-
if (labpo == 'left') and (colc0 == col0):
2212-
same += [axc]
2213-
elif (labpo == 'right') and (colc1 == col1):
2214-
same += [axc]
2215-
for axx in same:
2216-
_log.debug(' Same: %s', axx.yaxis.label)
2217-
axx.yaxis._align_label_siblings += [ax.yaxis]
2196+
if (((labpo == 'left') and (colc0 == col0)) or
2197+
((labpo == 'right') and (colc1 == col1))):
2198+
axc.yaxis._align_label_siblings += [ax.yaxis]
22182199

22192200
# place holder until #9498 is merged...
22202201
def align_titles(self, axs=None, renderer=None):
@@ -2268,7 +2249,7 @@ def align_titles(self, axs=None, renderer=None):
22682249
ax.title.set_position(x0, y)
22692250
y0 = y
22702251
elif y0 > y:
2271-
axx.title.set_positions(x, y0)
2252+
axx.title.set_position(x, y0)
22722253

22732254
def align_labels(self, axs=None, renderer=None):
22742255
"""
@@ -2290,9 +2271,8 @@ def align_labels(self, axs=None, renderer=None):
22902271
22912272
matplotlib.figure.Figure.align_ylabels
22922273
"""
2293-
self.align_xlabels(axs=axs, renderer=renderer)
2294-
self.align_ylabels(axs=axs, renderer=renderer)
2295-
# self.align_titles(axs=axs, renderer=renderer)
2274+
self.align_xlabels(axs=axs)
2275+
self.align_ylabels(axs=axs)
22962276

22972277

22982278
def figaspect(arg):

0 commit comments

Comments
 (0)