@@ -132,10 +132,14 @@ def __init__(self, **kwargs):
132132 self ._supxlabel = None
133133 self ._supylabel = None
134134
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 align.
136+ # see self.align_xlabels, self.align_ylabels,
137+ # self.align_titles, and axis._get_tick_boxes_siblings
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 = []
@@ -1293,7 +1297,7 @@ def subplots_adjust(self, left=None, bottom=None, right=None, top=None,
12931297
12941298 def align_xlabels (self , axs = None ):
12951299 """
1296- Align the xlabels of subplots in the same subplot column if label
1300+ Align the xlabels of subplots in the same subplot row if label
12971301 alignment is being done automatically (i.e. the label position is
12981302 not manually set).
12991303
@@ -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,53 @@ 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 row 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+ for ax in axs :
1461+ _log .debug (' Working on: %s' , ax .get_title ())
1462+ rowspan = ax .get_subplotspec ().rowspan
1463+ for axc in axs :
1464+ rowspanc = axc .get_subplotspec ().rowspan
1465+ if (rowspan .start == rowspanc .start ):
1466+ self ._align_label_groups ['title' ].join (ax , axc )
1467+
14151468 def align_labels (self , axs = None ):
14161469 """
14171470 Align the xlabels and ylabels of subplots with the same subplots
@@ -1430,8 +1483,8 @@ def align_labels(self, axs=None):
14301483 See Also
14311484 --------
14321485 matplotlib.figure.Figure.align_xlabels
1433-
14341486 matplotlib.figure.Figure.align_ylabels
1487+ matplotlib.figure.Figure.align_titles
14351488 """
14361489 self .align_xlabels (axs = axs )
14371490 self .align_ylabels (axs = axs )
0 commit comments