Align the ylabels of subplots in the same subplot column 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 left, it is aligned with lab
(self, axs=None)
| 1425 | self._align_label_groups['x'].join(ax, axc) |
| 1426 | |
| 1427 | def align_ylabels(self, axs=None): |
| 1428 | """ |
| 1429 | Align the ylabels of subplots in the same subplot column if label |
| 1430 | alignment is being done automatically (i.e. the label position is |
| 1431 | not manually set). |
| 1432 | |
| 1433 | Alignment persists for draw events after this is called. |
| 1434 | |
| 1435 | If a label is on the left, it is aligned with labels on Axes that |
| 1436 | also have their label on the left and that have the same |
| 1437 | left-most subplot column. If the label is on the right, |
| 1438 | it is aligned with labels on Axes with the same right-most column. |
| 1439 | |
| 1440 | Parameters |
| 1441 | ---------- |
| 1442 | axs : list of `~matplotlib.axes.Axes` |
| 1443 | Optional list (or `~numpy.ndarray`) of `~matplotlib.axes.Axes` |
| 1444 | to align the ylabels. |
| 1445 | Default is to align all Axes on the figure. |
| 1446 | |
| 1447 | See Also |
| 1448 | -------- |
| 1449 | matplotlib.figure.Figure.align_xlabels |
| 1450 | matplotlib.figure.Figure.align_titles |
| 1451 | matplotlib.figure.Figure.align_labels |
| 1452 | |
| 1453 | Notes |
| 1454 | ----- |
| 1455 | This assumes that all Axes in ``axs`` are from the same `.GridSpec`, |
| 1456 | so that their `.SubplotSpec` positions correspond to figure positions. |
| 1457 | |
| 1458 | Examples |
| 1459 | -------- |
| 1460 | Example with large yticks labels:: |
| 1461 | |
| 1462 | fig, axs = plt.subplots(2, 1) |
| 1463 | axs[0].plot(np.arange(0, 1000, 50)) |
| 1464 | axs[0].set_ylabel('YLabel 0') |
| 1465 | axs[1].set_ylabel('YLabel 1') |
| 1466 | fig.align_ylabels() |
| 1467 | """ |
| 1468 | if axs is None: |
| 1469 | axs = self.axes |
| 1470 | axs = [ax for ax in np.ravel(axs) if ax.get_subplotspec() is not None] |
| 1471 | for ax in axs: |
| 1472 | _log.debug(' Working on: %s', ax.get_ylabel()) |
| 1473 | colspan = ax.get_subplotspec().colspan |
| 1474 | pos = ax.yaxis.get_label_position() # left or right |
| 1475 | # Search through other Axes for label positions that are same as |
| 1476 | # this one and that share the appropriate column number. |
| 1477 | # Add to a list associated with each Axes of siblings. |
| 1478 | # This list is inspected in `axis.draw` by |
| 1479 | # `axis._update_label_position`. |
| 1480 | for axc in axs: |
| 1481 | if axc.yaxis.get_label_position() == pos: |
| 1482 | colspanc = axc.get_subplotspec().colspan |
| 1483 | if (pos == 'left' and colspan.start == colspanc.start or |
| 1484 | pos == 'right' and colspan.stop == colspanc.stop): |