@@ -135,7 +135,11 @@ def __init__(self, **kwargs):
135
135
# groupers to keep track of x and y labels we want to align.
136
136
# see self.align_xlabels and self.align_ylabels and
137
137
# 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
+ }
139
143
140
144
self ._localaxes = [] # track all Axes
141
145
self .artists = []
@@ -1314,6 +1318,7 @@ def align_xlabels(self, axs=None):
1314
1318
See Also
1315
1319
--------
1316
1320
matplotlib.figure.Figure.align_ylabels
1321
+ matplotlib.figure.Figure.align_titles
1317
1322
matplotlib.figure.Figure.align_labels
1318
1323
1319
1324
Notes
@@ -1375,6 +1380,7 @@ def align_ylabels(self, axs=None):
1375
1380
See Also
1376
1381
--------
1377
1382
matplotlib.figure.Figure.align_xlabels
1383
+ matplotlib.figure.Figure.align_titles
1378
1384
matplotlib.figure.Figure.align_labels
1379
1385
1380
1386
Notes
@@ -1412,6 +1418,68 @@ def align_ylabels(self, axs=None):
1412
1418
# grouper for groups of ylabels to align
1413
1419
self ._align_label_groups ['y' ].join (ax , axc )
1414
1420
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
+
1415
1483
def align_labels (self , axs = None ):
1416
1484
"""
1417
1485
Align the xlabels and ylabels of subplots with the same subplots
@@ -1435,6 +1503,7 @@ def align_labels(self, axs=None):
1435
1503
"""
1436
1504
self .align_xlabels (axs = axs )
1437
1505
self .align_ylabels (axs = axs )
1506
+ self .align_titles (axs = axs )
1438
1507
1439
1508
def add_gridspec (self , nrows = 1 , ncols = 1 , ** kwargs ):
1440
1509
"""
0 commit comments