|
1 | 1 | """
|
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. |
5 | 8 |
|
6 |
| -Align the axis labels between subplots. |
7 | 9 | """
|
8 | 10 | importnumpyasnp
|
9 | 11 | importmatplotlib.pyplotasplt
|
10 | 12 |
|
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) |
12 | 33 |
|
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) |
14 | 41 | 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() |
15 | 47 |
|
16 |
| -# Fixing random state for reproducibility |
17 |
| -np.random.seed(19680801) |
| 48 | +############################################################################# |
18 | 49 |
|
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. |
23 | 59 |
|
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) |
26 | 62 |
|
27 |
| -labelx=-0.3# axes coords |
| 63 | +make_plot(axs) |
28 | 64 |
|
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 |
34 | 66 |
|
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) |
38 | 69 |
|
39 | 70 | plt.show()
|
40 | 71 |
|
|
49 | 80 | # in this example:
|
50 | 81 |
|
51 | 82 | importmatplotlib
|
| 83 | +matplotlib.figure.Figure.align_ylabels |
52 | 84 | matplotlib.axis.Axis.set_label_coords
|
53 | 85 | matplotlib.axes.Axes.plot
|
54 | 86 | matplotlib.pyplot.plot
|
|