Skip to content

Commit 2ff72be

Browse files
committed
Add documentation for align_titles
- Add align_titles to api doc
1 parent ecf1553 commit 2ff72be

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

doc/api/figure_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Annotating
7171
Figure.align_labels
7272
Figure.align_xlabels
7373
Figure.align_ylabels
74+
Figure.align_titles
7475
Figure.autofmt_xdate
7576

7677

@@ -264,6 +265,7 @@ Annotating
264265
SubFigure.align_labels
265266
SubFigure.align_xlabels
266267
SubFigure.align_ylabels
268+
SubFigure.align_titles
267269

268270
Adding and getting Artists
269271
--------------------------
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
subplot titles can now be automatically aligned
2+
-----------------------------------------------
3+
4+
Subplot axes titles can be misaligned vertically if tick labels or
5+
xlabels are placed at the top of one subplot. The new method on the
6+
`.Figure` class: `.Figure.align_titles` will now align the titles
7+
vertically.
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
"""
2-
===============
3-
Aligning Labels
4-
===============
2+
==========================
3+
Aligning Labels and Titles
4+
==========================
55
6-
Aligning xlabel and ylabel using `.Figure.align_xlabels` and
7-
`.Figure.align_ylabels`
6+
Aligning xlabel, ylabel, and title using `.Figure.align_xlabels`,
7+
`.Figure.align_ylabels`, and `.Figure.align_titles`.
88
9-
`.Figure.align_labels` wraps these two functions.
9+
`.Figure.align_labels` wraps the x and y label functions.
1010
1111
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.
12+
x-axis, "YLabel0 0" would be much closer to the y-axis, and title
13+
"Title0 0" would be much closer to the top of their respective axes.
1414
"""
1515
import matplotlib.pyplot as plt
1616
import numpy as np
1717

18-
import matplotlib.gridspec as gridspec
18+
fig, axs = plt.subplots(2, 2, layout='tight')
1919

20-
fig = plt.figure(tight_layout=True)
21-
gs = gridspec.GridSpec(2, 2)
22-
23-
ax = fig.add_subplot(gs[0, :])
20+
ax = axs[0][0]
2421
ax.plot(np.arange(0, 1e6, 1000))
25-
ax.set_ylabel('YLabel0')
26-
ax.set_xlabel('XLabel0')
22+
ax.set_title('Title0 0')
23+
ax.set_ylabel('YLabel0 0')
24+
25+
ax = axs[0][1]
26+
ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
27+
ax.set_title('Title0 1')
28+
ax.xaxis.tick_top()
29+
ax.tick_params(axis='x', rotation=55)
30+
2731

2832
for i in range(2):
29-
ax = fig.add_subplot(gs[1, i])
33+
ax = axs[1][i]
3034
ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
3135
ax.set_ylabel('YLabel1 %d' % i)
3236
ax.set_xlabel('XLabel1 %d' % i)
3337
if i == 0:
3438
ax.tick_params(axis='x', rotation=55)
39+
3540
fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels()
41+
fig.align_titles()
3642

3743
plt.show()

lib/matplotlib/figure.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def __init__(self, **kwargs):
132132
self._supxlabel = None
133133
self._supylabel = None
134134

135-
# groupers to keep track of x, y labels and title we want to
136-
# align.
135+
# groupers to keep track of x, y labels and title we want to align.
137136
# see self.align_xlabels, self.align_ylabels,
138137
# self.align_titles, and axis._get_tick_boxes_siblings
139138
self._align_label_groups = {

0 commit comments

Comments
 (0)