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

Skip to content

Commit 1447ed9

Browse files
committed
Refinements
1 parent ce91095 commit 1447ed9

File tree

4 files changed

+53
-112
lines changed

4 files changed

+53
-112
lines changed

doc/users/next_whats_new/2017-11-1_figure_align_labels.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Only labels that have the same subplot locations are aligned. i.e. the
1313
ylabels are aligned only if the subplots are in the same column of the
1414
subplot layout.
1515

16+
Alignemnt is persistent and automatic after these are called.
17+
1618
A convenience wrapper `Figure.align_labels` calls both functions at once.
1719

1820
.. plot::

examples/subplots_axes_and_figures/align_labels_demo.py

Lines changed: 5 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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 its 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

Lines changed: 44 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,9 +2084,13 @@ 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
"""
2089-
Align the xlabels of subplots in this figure.
2089+
Align the ylabels of subplots in the same subplot column if label
2090+
alignment is being done automatically (i.e. the label position is
2091+
not manually set).
2092+
2093+
Alignment persists for draw events after this is called.
20902094
20912095
If a label is on the bottom, it is aligned with labels on axes that
20922096
also have their label on the bottom and that have the same
@@ -2096,11 +2100,8 @@ def align_xlabels(self, axs=None, renderer=None):
20962100
Parameters
20972101
----------
20982102
axs : list of `~matplotlib.axes.Axes` (None)
2099-
Optional list of `~matplotlib.axes.Axes` to align
2100-
the xlabels.
2101-
2102-
renderer : (None)
2103-
Optional renderer to do the adjustment on.
2103+
Optional list of (or ndarray) `~matplotlib.axes.Axes` to align
2104+
the xlabels. Default is to align all axes on the figure.
21042105
21052106
See Also
21062107
--------
@@ -2121,11 +2122,6 @@ def align_xlabels(self, axs=None, renderer=None):
21212122
21222123
"""
21232124

2124-
from .tight_layout import get_renderer
2125-
2126-
if renderer is None:
2127-
renderer = get_renderer(self)
2128-
21292125
if axs is None:
21302126
axs = self.axes
21312127

@@ -2135,25 +2131,30 @@ def align_xlabels(self, axs=None, renderer=None):
21352131
_log.debug(' Working on: %s', ax.get_xlabel())
21362132
ss = ax.get_subplotspec()
21372133
nrows, ncols, row0, row1, col0, col1 = ss.get_rows_columns()
2138-
same = [ax]
2139-
labpo = ax.xaxis.get_label_position()
2134+
labpo = ax.xaxis.get_label_position() # top or bottom
2135+
2136+
# loop through other axes, and search for label positions
2137+
# that are same as this one, and that share the appropriate
2138+
# row number.
2139+
# Add to a list associated with each axes of sibblings.
2140+
# This list is inspected in `axis.draw` by
2141+
# `axis._update_label_position`.
21402142
for axc in axs:
21412143
if axc.xaxis.get_label_position() == labpo:
21422144
ss = axc.get_subplotspec()
21432145
nrows, ncols, rowc0, rowc1, colc, col1 = \
21442146
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]
2147+
if (labpo == 'bottom' and rowc1 == row1 or
2148+
labpo == 'top' and rowc0 == row0):
2149+
axc.xaxis._align_label_siblings += [ax.xaxis]
21532150

2154-
def align_ylabels(self, axs=None, renderer=None):
2151+
def align_ylabels(self, axs=None):
21552152
"""
2156-
Align the ylabels of subplots in this figure.
2153+
Align the ylabels of subplots in the same subplot column if label
2154+
alignment is being done automatically (i.e. the label position is
2155+
not manually set).
2156+
2157+
Alignment persists for draw events after this is called.
21572158
21582159
If a label is on the left, it is aligned with labels on axes that
21592160
also have their label on the left and that have the same
@@ -2163,11 +2164,8 @@ def align_ylabels(self, axs=None, renderer=None):
21632164
Parameters
21642165
----------
21652166
axs : list of `~matplotlib.axes.Axes` (None)
2166-
Optional list of `~matplotlib.axes.Axes` to align
2167-
the ylabels.
2168-
2169-
renderer : (None)
2170-
Optional renderer to do the adjustment on.
2167+
Optional list (or ndarray) of `~matplotlib.axes.Axes` to align
2168+
the ylabels. Default is to align all axes on the figure.
21712169
21722170
See Also
21732171
--------
@@ -2187,11 +2185,6 @@ def align_ylabels(self, axs=None, renderer=None):
21872185
21882186
"""
21892187

2190-
from .tight_layout import get_renderer
2191-
2192-
if renderer is None:
2193-
renderer = get_renderer(self)
2194-
21952188
if axs is None:
21962189
axs = self.axes
21972190

@@ -2202,97 +2195,44 @@ def align_ylabels(self, axs=None, renderer=None):
22022195
nrows, ncols, row0, row1, col0, col1 = ss.get_rows_columns()
22032196
same = [ax]
22042197
labpo = ax.yaxis.get_label_position()
2198+
# loop through other axes, and search for label positions
2199+
# that are same as this one, and that share the appropriate
2200+
# column number.
2201+
# Add to a list associated with each axes of sibblings.
2202+
# This list is inspected in `axis.draw` by
2203+
# `axis._update_label_position`.
22052204
for axc in axs:
22062205
if axc != ax:
22072206
if axc.yaxis.get_label_position() == labpo:
22082207
ss = axc.get_subplotspec()
22092208
nrows, ncols, row0, row1, colc0, colc1 = \
22102209
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]
2218-
2219-
# place holder until #9498 is merged...
2220-
def align_titles(self, axs=None, renderer=None):
2221-
"""
2222-
Align the titles of subplots in this figure.
2223-
2224-
Parameters
2225-
----------
2226-
axs : list of `~matplotlib.axes.Axes` (None)
2227-
Optional list of axes to align the xlabels.
2228-
2229-
renderer : (None)
2230-
Optional renderer to do the adjustment on.
2231-
2232-
See Also
2233-
--------
2234-
matplotlib.figure.Figure.align_xlabels
2235-
2236-
matplotlib.figure.Figure.align_ylabels
2237-
"""
2238-
2239-
from .tight_layout import get_renderer
2240-
2241-
if renderer is None:
2242-
renderer = get_renderer(self)
2243-
2244-
if axs is None:
2245-
axs = self.axes
2246-
2247-
while len(axs):
2248-
ax = axs.pop()
2249-
ax._update_title_position(renderer)
2250-
same = [ax]
2251-
if ax._autolabelpos:
2252-
ss = ax.get_subplotspec()
2253-
nrows, ncols, row0, row1, col0, col1 = ss.get_rows_columns()
2254-
labpo = ax.xaxis.get_label_position()
2255-
for axc in axs:
2256-
axc._update_title_position(renderer)
2257-
if axc._autolabelpos:
2258-
ss = axc.get_subplotspec()
2259-
nrows, ncols, rowc0, rowc1, colc, col1 = \
2260-
ss.get_rows_columns()
2261-
if (rowc0 == row0):
2262-
same += [axc]
2263-
2264-
x0, y0 = ax.title.get_position()
2265-
for axx in same:
2266-
x, y = axx.title.get_position()
2267-
if y > y0:
2268-
ax.title.set_position(x0, y)
2269-
y0 = y
2270-
elif y0 > y:
2271-
axx.title.set_positions(x, y0)
2210+
if (labpo == 'left' and colc0 == col0 or
2211+
labpo == 'right' and colc1 == col1):
2212+
axc.yaxis._align_label_siblings += [ax.yaxis]
22722213

2273-
def align_labels(self, axs=None, renderer=None):
2214+
def align_labels(self, axs=None):
22742215
"""
22752216
Align the xlabels and ylabels of subplots with the same subplots
2276-
row or column (respectively).
2217+
row or column (respectively) if label alignment is being
2218+
done automatically (i.e. the label position is not manually set).
2219+
2220+
Alignment persists for draw events after this is called.
22772221
22782222
Parameters
22792223
----------
22802224
axs : list of `~matplotlib.axes.Axes` (None)
22812225
Optional list (or ndarray) of `~matplotlib.axes.Axes` to
2282-
align the labels.
2283-
2284-
renderer : (None)
2285-
Optional renderer to do the adjustment on.
2226+
align the labels. Default is to align all axes on the figure.
22862227
22872228
See Also
22882229
--------
22892230
matplotlib.figure.Figure.align_xlabels
22902231
22912232
matplotlib.figure.Figure.align_ylabels
22922233
"""
2293-
self.align_xlabels(axs=axs, renderer=renderer)
2294-
self.align_ylabels(axs=axs, renderer=renderer)
2295-
# self.align_titles(axs=axs, renderer=renderer)
2234+
self.align_xlabels(axs=axs)
2235+
self.align_ylabels(axs=axs)
22962236

22972237

22982238
def figaspect(arg):

0 commit comments

Comments
 (0)