8000 Add align_titles based on closed PR #22793 · trananso/matplotlib@71f0312 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71f0312

Browse files
committed
Add align_titles based on closed PR matplotlib#22793
1 parent 2420855 commit 71f0312

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

lib/matplotlib/figure.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ def __init__(self, **kwargs):
135135
# groupers to keep track of x and y labels we want to align.
136136
# see self.align_xlabels and self.align_ylabels and
137137
# axis._get_tick_boxes_siblings
138-
self._align_label_groups = {"x": cbook.Grouper(), "y": cbook.Grouper()}
138+
self._align_label_groups = {
139+
"x": cbook.Grouper(),
140+
"y": cbook.Grouper(),
141+
"title": cbook.Grouper()
142+
}
139143

140144
self._localaxes = [] # track all Axes
141145
self.artists = []
@@ -1314,6 +1318,7 @@ def align_xlabels(self, axs=None):
13141318
See Also
13151319
--------
13161320
matplotlib.figure.Figure.align_ylabels
1321+
matplotlib.figure.Figure.align_titles
13171322
matplotlib.figure.Figure.align_labels
13181323
13191324
Notes
@@ -1375,6 +1380,7 @@ def align_ylabels(self, axs=None):
13751380
See Also
13761381
--------
13771382
matplotlib.figure.Figure.align_xlabels
1383+
matplotlib.figure.Figure.align_titles
13781384
matplotlib.figure.Figure.align_labels
13791385
13801386
Notes
@@ -1412,6 +1418,68 @@ def align_ylabels(self, axs=None):
14121418
# grouper for groups of ylabels to align
14131419
self._align_label_groups['y'].join(ax, axc)
14141420

1421+
def align_titles(self, axs=None):
1422+
"""
1423+
Align the titles of subplots in the same subplot column if title
1424+
alignment is being done automatically (i.e. the title position is
1425+
not manually set).
1426+
1427+
Alignment persists for draw events after this is called.
1428+
1429+
Parameters
1430+
----------
1431+
axs : list of `~matplotlib.axes.Axes`
1432+
Optional list of (or ndarray) `~matplotlib.axes.Axes`
1433+
to align the titles.
1434+
Default is to align all Axes on the figure.
1435+
1436+
See Also
1437+
--------
1438+
matplotlib.figure.Figure.align_xlabels
1439+
matplotlib.figure.Figure.align_ylabels
1440+
matplotlib.figure.Figure.align_labels
1441+
1442+
Notes
1443+
-----
1444+
This assumes that ``axs`` are from the same `.GridSpec`, so that
1445+
their `.SubplotSpec` positions correspond to figure positions.
1446+
1447+
Examples
1448+
--------
1449+
Example with titles::
1450+
1451+
fig, axs = plt.subplots(1, 2)
1452+
axs[0].set_aspect('equal')
1453+
axs[0].set_title('Title 0')
1454+
axs[1].set_title('Title 1')
1455+
fig.align_titles()
1456+
"""
1457+
# if axs is None:
1458+
# axs = self.axes
1459+
# axs = [ax for ax in np.ravel(axs) if ax.get_subplotspec() is not None]
1460+
# locs = ['left', 'center', 'right']
1461+
# for ax in axs:
1462+
# for loc in locs:
1463+
# if ax.get_title(loc=loc):
1464+
# _log.debug(' Working on: %s', ax.get_title(loc=loc))
1465+
# rowspan = ax.get_subplotspec().rowspan
1466+
# for axc in axs:
1467+
# for loc in locs:
1468+
# rowspanc = axc.get_subplotspec().rowspan
1469+
# if rowspan.start == rowspanc.start or \
1470+
# rowspan.stop == rowspanc.stop:
1471+
# self._align_label_groups['title'].join(ax, axc)
1472+
if axs is None:
1473+
axs = self.axes
1474+
axs = [ax for ax in np.ravel(axs) if ax.get_subplotspec() is not None]
1475+
for ax in axs:
1476+
_log.debug(' Working on: %s', ax.get_title())
1477+
rowspan = ax.get_subplotspec().rowspan
1478+
for axc in axs:
1479+
rowspanc = axc.get_subplotspec().rowspan
1480+
if (rowspan.start == rowspanc.start):
1481+
self._align_label_groups['title'].join(ax, axc)
1482+
14151483
def align_labels(self, axs=None):
14161484
"""
14171485
Align the xlabels and ylabels of subplots with the same subplots
@@ -1435,6 +1503,7 @@ def align_labels(self, axs=None):
14351503
"""
14361504
self.align_xlabels(axs=axs)
14371505
self.align_ylabels(axs=axs)
1506+
self.align_titles(axs=axs)
14381507

14391508
def add_gridspec(self, nrows=1, ncols=1, **kwargs):
14401509
"""

0 commit comments

Comments
 (0)
0