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 b078643

Browse filesBrowse files
authored
Merge pull request #11424 from jklymak/doc-align-ylabels
DOC: point align-ylabel demo to new align-label functions
2 parents 2b51fa3 + 07501bd commit b078643
Copy full SHA for b078643

File tree

Expand file treeCollapse file tree

1 file changed

+55
-23
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+55
-23
lines changed

‎examples/pyplots/align_ylabels.py

Copy file name to clipboardExpand all lines: examples/pyplots/align_ylabels.py
+55-23Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,71 @@
11
"""
2-
=============
3-
Align Ylabels
4-
=============
2+
==============
3+
Align y-labels
4+
==============
5+
6+
Two methods are shown here, one using a short call to `.Figure.align_ylabels`
7+
and the second a manual way to align the labels.
58
6-
Align the axis labels between subplots.
79
"""
810
import numpy as np
911
import matplotlib.pyplot as plt
1012

11-
box = dict(facecolor='yellow', pad=5, alpha=0.2)
13+
def make_plot(axs):
14+
box = dict(facecolor='yellow', pad=5, alpha=0.2)
15+
16+
# Fixing random state for reproducibility
17+
np.random.seed(19680801)
18+
ax1 = axs[0, 0]
19+
ax1.plot(2000*np.random.rand(10))
20+
ax1.set_title('ylabels not aligned')
21+
ax1.set_ylabel('misaligned 1', bbox=box)
22+
ax1.set_ylim(0, 2000)
23+
24+
ax3 = axs[1, 0]
25+
ax3.set_ylabel('misaligned 2',bbox=box)
26+
ax3.plot(np.random.rand(10))
27+
28+
ax2 = axs[0, 1]
29+
ax2.set_title('ylabels aligned')
30+
ax2.plot(2000*np.random.rand(10))
31+
ax2.set_ylabel('aligned 1', bbox=box)
32+
ax2.set_ylim(0, 2000)
1233

13-
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
34+
ax4 = axs[1, 1]
35+
ax4.plot(np.random.rand(10))
36+
ax4.set_ylabel('aligned 2', bbox=box)
37+
38+
# Plot 1:
39+
40+
fig, axs = plt.subplots(2, 2)
1441
fig.subplots_adjust(left=0.2, wspace=0.6)
42+
make_plot(axs)
43+
44+
# just align the last column of axes:
45+
fig.align_ylabels(axs[:,1])
46+
plt.show()
1547

16-
# Fixing random state for reproducibility
17-
np.random.seed(19680801)
48+
#############################################################################
49+
#
50+
# .. seealso::
51+
# `.Figure.align_ylabels` and `.Figure.align_labels` for a direct method
52+
# of doing the same thing.
53+
# Also :doc:`/gallery/subplots_axes_and_figures/align_labels_demo`
54+
#
55+
#
56+
# Or we can manually align the axis labels between subplots manually using the
57+
# `set_label_coords` method of the y-axis object. Note this requires we know
58+
# a good offset value which is hardcoded.
1859

19-
ax1.plot(2000*np.random.rand(10))
20-
ax1.set_title('ylabels not aligned')
21-
ax1.set_ylabel('misaligned 1', bbox=box)
22-
ax1.set_ylim(0, 2000)
60+
fig, axs = plt.subplots(2, 2)
61+
fig.subplots_adjust(left=0.2, wspace=0.6)
2362

24-
ax3.set_ylabel('misaligned 2',bbox=box)
25-
ax3.plot(np.random.rand(10))
63+
make_plot(axs)
2664

2765
labelx = -0.3 # axes coords
2866

29-
ax2.set_title('ylabels aligned')
30-
ax2.plot(2000*np.random.rand(10))
31-
ax2.set_ylabel('aligned 1', bbox=box)
32-
ax2.yaxis.set_label_coords(labelx, 0.5)
33-
ax2.set_ylim(0, 2000)
34-
35-
ax4.plot(np.random.rand(10))
36-
ax4.set_ylabel('aligned 2', bbox=box)
37-
ax4.yaxis.set_label_coords(labelx, 0.5)
67+
for j in range(2):
68+
axs[j, 1].yaxis.set_label_coords(labelx, 0.5)
3869

3970
plt.show()
4071

@@ -49,6 +80,7 @@
4980
# in this example:
5081

5182
import matplotlib
83+
matplotlib.figure.Figure.align_ylabels
5284
matplotlib.axis.Axis.set_label_coords
5385
matplotlib.axes.Axes.plot
5486
matplotlib.pyplot.plot

0 commit comments

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