MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / align_xlabels

Method align_xlabels

lib/matplotlib/figure.py:1366–1425  ·  view source on GitHub ↗

Align the xlabels of subplots in the same subplot row if label alignment is being done automatically (i.e. the label position is not manually set). Alignment persists for draw events after this is called. If a label is on the bottom, it is aligned with labe

(self, axs=None)

Source from the content-addressed store, hash-verified

1364 self.stale = True
1365
1366 def align_xlabels(self, axs=None):
1367 """
1368 Align the xlabels of subplots in the same subplot row if label
1369 alignment is being done automatically (i.e. the label position is
1370 not manually set).
1371
1372 Alignment persists for draw events after this is called.
1373
1374 If a label is on the bottom, it is aligned with labels on Axes that
1375 also have their label on the bottom and that have the same
1376 bottom-most subplot row. If the label is on the top,
1377 it is aligned with labels on Axes with the same top-most row.
1378
1379 Parameters
1380 ----------
1381 axs : list of `~matplotlib.axes.Axes`
1382 Optional list of (or `~numpy.ndarray`) `~matplotlib.axes.Axes`
1383 to align the xlabels.
1384 Default is to align all Axes on the figure.
1385
1386 See Also
1387 --------
1388 matplotlib.figure.Figure.align_ylabels
1389 matplotlib.figure.Figure.align_titles
1390 matplotlib.figure.Figure.align_labels
1391
1392 Notes
1393 -----
1394 This assumes that all Axes in ``axs`` are from the same `.GridSpec`,
1395 so that their `.SubplotSpec` positions correspond to figure positions.
1396
1397 Examples
1398 --------
1399 Example with rotated xtick labels::
1400
1401 fig, axs = plt.subplots(1, 2)
1402 axs[0].tick_params(axis='x', rotation=55)
1403 axs[0].set_xlabel('XLabel 0')
1404 axs[1].set_xlabel('XLabel 1')
1405 fig.align_xlabels()
1406 """
1407 if axs is None:
1408 axs = self.axes
1409 axs = [ax for ax in np.ravel(axs) if ax.get_subplotspec() is not None]
1410 for ax in axs:
1411 _log.debug(' Working on: %s', ax.get_xlabel())
1412 rowspan = ax.get_subplotspec().rowspan
1413 pos = ax.xaxis.get_label_position() # top or bottom
1414 # Search through other Axes for label positions that are same as
1415 # this one and that share the appropriate row number.
1416 # Add to a grouper associated with each Axes of siblings.
1417 # This list is inspected in `axis.draw` by
1418 # `axis._update_label_position`.
1419 for axc in axs:
1420 if axc.xaxis.get_label_position() == pos:
1421 rowspanc = axc.get_subplotspec().rowspan
1422 if (pos == 'top' and rowspan.start == rowspanc.start or
1423 pos == 'bottom' and rowspan.stop == rowspanc.stop):

Callers 3

align_labelsMethod · 0.95

Calls 4

get_xlabelMethod · 0.80
joinMethod · 0.80
get_subplotspecMethod · 0.45
get_label_positionMethod · 0.45

Tested by 1