Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5c8c91

Browse filesBrowse files
committed
Consolidate align_labels_demo and align_ylabels examples
1 parent ae47a6e commit f5c8c91
Copy full SHA for f5c8c91

File tree

3 files changed

+49
-112
lines changed
Filter options

3 files changed

+49
-112
lines changed

‎doc/users/faq.rst

Copy file name to clipboardExpand all lines: doc/users/faq.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ vertically across the multiple subplots, which can be unattractive.
205205
By default, Matplotlib positions the x location of the ylabel so that
206206
it does not overlap any of the y ticks. You can override this default
207207
behavior by specifying the coordinates of the label. To learn how, see
208-
:doc:`/gallery/text_labels_and_annotations/align_ylabels`
208+
:doc:`/gallery/subplots_axes_and_figures/align_labels_demo`
209209

210210
.. _howto-set-zorder:
211211

‎galleries/examples/subplots_axes_and_figures/align_labels_demo.py

Copy file name to clipboardExpand all lines: galleries/examples/subplots_axes_and_figures/align_labels_demo.py
+48-25Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,65 @@
88
99
`.Figure.align_labels` wraps the x and y label functions.
1010
11-
Note that the xlabel "XLabel1 1" would normally be much closer to the
12-
x-axis, "YLabel0 0" would be much closer to the y-axis, and title
13-
"Title0 0" would be much closer to the top of their respective axes.
11+
We align the xlabels and ylabels using short calls to `.Figure.align_xlabels`
12+
and `.Figure.align_ylabels`. We also show a manual way to align the ylabels
13+
using the `~.Axis.set_label_coords` method of the yaxis object. Note this requires
14+
knowing a good offset value which is hardcoded.
15+
16+
.. redirect-from:: /gallery/pyplots/align_ylabels
1417
"""
18+
1519
import matplotlib.pyplot as plt
1620
import numpy as np
1721

18-
fig, axs = plt.subplots(2, 2, layout='constrained')
19-
20-
ax = axs[0][0]
21-
ax.plot(np.arange(0, 1e6, 1000))
22-
ax.set_title('Title0 0')
23-
ax.set_ylabel('YLabel0 0')
22+
fig, axs = plt.subplots(2, 3, figsize=(8.9, 5.5),
23+
layout='constrained', gridspec_kw={'wspace': 0.1})
2424

25-
ax = axs[0][1]
26-
ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
27-
ax.set_title('Title0 1')
28-
ax.xaxis.tick_top()
29-
ax.set_xticks(ax.get_xticks())
30-
ax.tick_params(axis='x', rotation=55, rotation_mode='xtick')
25+
# add sample data and labels
26+
for ax in axs.flat:
27+
scale = 2000 if ax.get_subplotspec().is_first_row() else 1
28+
ax.plot(scale * (1 - np.exp(-np.linspace(0, 5, 100))))
29+
if ax.get_subplotspec().is_last_row():
30+
ax.set_xlabel('xlabel', bbox=dict(facecolor='yellow', pad=5, alpha=0.2))
31+
ax.set_ylabel('ylabel', bbox=dict(facecolor='yellow', pad=5, alpha=0.2))
32+
ax.set_ylim(0, scale)
3133

34+
# Modify ticks to get different margins in some plots
35+
axs[0, 0].xaxis.tick_top()
36+
axs[1, 2].tick_params(axis='x', rotation=55)
37+
axs[0, 0].set_title('ylabels not aligned')
3238

33-
for i in range(2):
34-
ax = axs[1][i]
35-
ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
36-
ax.set_ylabel('YLabel1 %d' % i)
37-
ax.set_xlabel('XLabel1 %d' % i)
38-
if i == 0:
39-
ax.set_xticks(ax.get_xticks())
40-
ax.tick_params(axis='x', rotation=55, rotation_mode='xtick')
39+
# Align labels
40+
fig.align_titles() # Align titles
41+
fig.align_xlabels() # Align all x-axis labels
42+
fig.align_ylabels(axs[:, 1]) # Align only the second column's y-labels
43+
axs[0, 1].set_title('fig.align_ylabels()')
4144

42-
fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels()
43-
fig.align_titles()
45+
# Manually adjust y-labels for the third column
46+
for ax in axs[:, 2]:
47+
ax.yaxis.set_label_coords(-0.3, 0.5)
48+
axs[0, 2].set_title('ylabels manually aligned')
4449

4550
plt.show()
4651

52+
53+
# %%
54+
#
55+
# .. admonition:: References
56+
#
57+
# The use of the following functions, methods, classes and modules is shown
58+
# in this example:
59+
#
60+
# - `matplotlib.figure.Figure.align_xlabels`
61+
# - `matplotlib.figure.Figure.align_ylabels`
62+
# - `matplotlib.figure.Figure.align_labels`
63+
# - `matplotlib.figure.Figure.align_titles`
64+
# - `matplotlib.axis.Axis.set_label_coords`
65+
# - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`
66+
# - `matplotlib.axes.Axes.set_title`
67+
# - `matplotlib.axes.Axes.set_ylabel`
68+
# - `matplotlib.axes.Axes.set_ylim`
69+
4770
# %%
4871
# .. tags::
4972
#

‎galleries/examples/text_labels_and_annotations/align_ylabels.py

Copy file name to clipboardExpand all lines: galleries/examples/text_labels_and_annotations/align_ylabels.py
-86Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.