Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit3c27acc

Browse files
committed
DOC: point align-ylabel demo to new align-label functions
1 parented0a579 commit3c27acc

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

‎examples/pyplots/align_ylabels.py

Lines 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
importnumpyasnp
911
importmatplotlib.pyplotasplt
1012

11-
box=dict(facecolor='yellow',pad=5,alpha=0.2)
13+
defmake_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+
#############################################################################
1849

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)
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.
2359

24-
ax3.set_ylabel('misaligned 2',bbox=box)
25-
ax3.plot(np.random.rand(10))
60+
fig,axs=plt.subplots(2,2)
61+
fig.subplots_adjust(left=0.2,wspace=0.6)
2662

27-
labelx=-0.3# axes coords
63+
make_plot(axs)
2864

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)
65+
labelx=-0.3# axes coords
3466

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+
forjinrange(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
importmatplotlib
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)

[8]ページ先頭

©2009-2025 Movatter.jp