@@ -132,10 +132,15 @@ def __init__(self, **kwargs):
132
132
self ._supxlabel = None
133
133
self ._supylabel = None
134
134
135
- # groupers to keep track of x and y labels we want to align.
136
- # see self.align_xlabels and self.align_ylabels and
137
- # axis._get_tick_boxes_siblings
138
- self ._align_label_groups = {"x" : cbook .Grouper (), "y" : cbook .Grouper ()}
135
+ # groupers to keep track of x, y labels and title we want to
136
+ # align.
137
+ # see self.align_xlabels, self.align_ylabels,
138
+ # self.align_titles, and axis._get_tick_boxes_siblings
139
+ self ._align_label_groups = {
140
+ "x" : cbook .Grouper (),
141
+ "y" : cbook .Grouper (),
142
+ "title" : cbook .Grouper ()
143
+ }
139
144
140
145
self ._localaxes = [] # track all Axes
141
146
self .artists = []
@@ -1293,7 +1298,7 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None,
1293
1298
1294
1299
def align_xlabels (self , axs = None ):
1295
1300
"""
1296
- Align the xlabels of subplots in the same subplot column if label
1301
+ Align the xlabels of subplots in the same subplot row if label
1297
1302
alignment is being done automatically (i.e. the label position is
1298
1303
not manually set).
1299
1304
@@ -1314,6 +1319,7 @@ def align_xlabels(self, axs=None):
1314
1319
See Also
1315
1320
--------
1316
1321
matplotlib.figure.Figure.align_ylabels
1322
+ matplotlib.figure.Figure.align_titles
1317
1323
matplotlib.figure.Figure.align_labels
1318
1324
1319
1325
Notes
@@ -1375,6 +1381,7 @@ def align_ylabels(self, axs=None):
1375
1381
See Also
1376
1382
--------
1377
1383
matplotlib.figure.Figure.align_xlabels
1384
+ matplotlib.figure.Figure.align_titles
1378
1385
matplotlib.figure.Figure.align_labels
1379
1386
1380
1387
Notes
@@ -1412,6 +1419,53 @@ def align_ylabels(self, axs=None):
1412
1419
# grouper for groups of ylabels to align
1413
1420
self ._align_label_groups ['y' ].join (ax , axc )
1414
1421
1422
+ def align_titles (self , axs = None ):
1423
+ """
1424
+ Align the titles of subplots in the same subplot row if title
1425
+ alignment is being done automatically (i.e. the title position is
1426
+ not manually set).
1427
+
1428
+ Alignment persists for draw events after this is called.
1429
+
1430
+ Parameters
1431
+ ----------
1432
+ axs : list of `~matplotlib.axes.Axes`
1433
+ Optional list of (or ndarray) `~matplotlib.axes.Axes`
1434
+ to align the titles.
1435
+ Default is to align all Axes on the figure.
1436
+
1437
+ See Also
1438
+ --------
1439
+ matplotlib.figure.Figure.align_xlabels
1440
+ matplotlib.figure.Figure.align_ylabels
1441
+ matplotlib.figure.Figure.align_labels
1442
+
1443
+ Notes
1444
+ -----
1445
+ This assumes that ``axs`` are from the same `.GridSpec`, so that
1446
+ their `.SubplotSpec` positions correspond to figure positions.
1447
+
1448
+ Examples
1449
+ --------
1450
+ Example with titles::
1451
+
1452
+ fig, axs = plt.subplots(1, 2)
1453
+ axs[0].set_aspect('equal')
1454
+ axs[0].set_title('Title 0')
1455
+ axs[1].set_title('Title 1')
1456
+ fig.align_titles()
1457
+ """
1458
+ if axs is None :
1459
+ axs = self .axes
1460
+ axs = [ax for ax in np .ravel (axs ) if ax .get_subplotspec () is not None ]
1461
+ for ax in axs :
1462
+ _log .debug (' Working on: %s' , ax .get_title ())
1463
+ rowspan = ax .get_subplotspec ().rowspan
1464
+ for axc in axs :
1465
+ rowspanc = axc .get_subplotspec ().rowspan
1466
+ if (rowspan .start == rowspanc .start ):
1467
+ self ._align_label_groups ['title' ].join (ax , axc )
1468
+
1415
1469
def align_labels (self , axs = None ):
1416
1470
"""
1417
1471
Align the xlabels and ylabels of subplots with the same subplots
@@ -1430,8 +1484,8 @@ def align_labels(self, axs=None):
1430
1484
See Also
1431
1485
--------
1432
1486
matplotlib.figure.Figure.align_xlabels
1433
-
1434
1487
matplotlib.figure.Figure.align_ylabels
1488
+ matplotlib.figure.Figure.align_titles
1435
1489
"""
1436
1490
self .align_xlabels (axs = axs )
1437
1491
self .align_ylabels (axs = axs )
0 commit comments