|
3 | 3 | Labelling subplots
|
4 | 4 | ==================
|
5 | 5 |
|
6 |
| -Labelling subplots is relatively straightforward, and varies, |
7 |
| -so Matplotlibdoes not have a general method for doing this. |
| 6 | +Labelling subplots is relatively straightforward, and varies, so Matplotlib |
| 7 | +does not have a general method for doing this. |
8 | 8 |
|
9 |
| -Simplest is putting the label inside the axes. Note, here |
10 |
| -we use `.pyplot.subplot_mosaic`, and use the subplot labels |
11 |
| -as keys for the subplots, which is a nice convenience. However, |
12 |
| -the same method works with `.pyplot.subplots` or keys that are |
13 |
| -different than what you want to label the subplot with. |
| 9 | +The examples here rely on the ability of `~.Axes.annotate` to conveniently |
| 10 | +position text at a given physical offset (in fontsize units or in points) away |
| 11 | +from a corner of the Axes. |
| 12 | +
|
| 13 | +Note, here we use `.pyplot.subplot_mosaic`, and use the subplot labels as keys |
| 14 | +for the subplots, which is a nice convenience. However, the same method works |
| 15 | +with `.pyplot.subplots` or keys that are different than what you want to label |
| 16 | +the subplot with. |
14 | 17 | """
|
15 | 18 |
|
16 | 19 | importmatplotlib.pyplotasplt
|
17 | 20 |
|
18 |
| -importmatplotlib.transformsasmtransforms |
19 |
| - |
| 21 | +# %% |
20 | 22 | fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
|
21 | 23 | layout='constrained')
|
22 |
| - |
23 | 24 | forlabel,axinaxs.items():
|
24 |
| -# label physical distance in and down: |
25 |
| -trans=mtransforms.ScaledTranslation(10/72,-5/72,fig.dpi_scale_trans) |
26 |
| -ax.text(0.0,1.0,label,transform=ax.transAxes+trans, |
27 |
| -fontsize='medium',verticalalignment='top',fontfamily='serif', |
28 |
| -bbox=dict(facecolor='0.7',edgecolor='none',pad=3.0)) |
29 |
| - |
30 |
| -plt.show() |
| 25 | +# Put the label |
| 26 | +# - at the top left corner (axes fraction (0, 1)), |
| 27 | +# - offset half-a-fontsize right and half-a-fontsize down |
| 28 | +# (offset fontsize (+0.5, -0.5)), |
| 29 | +# i.e. just inside the axes. |
| 30 | +ax.annotate( |
| 31 | +label, |
| 32 | +xy=(0,1),xycoords='axes fraction', |
| 33 | +xytext=(+0.5,-0.5),textcoords='offset fontsize', |
| 34 | +fontsize='medium',verticalalignment='top',fontfamily='serif', |
| 35 | +bbox=dict(facecolor='0.7',edgecolor='none',pad=3.0)) |
31 | 36 |
|
32 | 37 | # %%
|
33 |
| -# We may prefer the labels outside the axes, but still aligned |
34 |
| -# with each other, in which case we use a slightly different transform: |
35 |
| - |
36 | 38 | fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
|
37 | 39 | layout='constrained')
|
38 |
| - |
39 | 40 | forlabel,axinaxs.items():
|
40 |
| -# label physical distance to the left and up: |
41 |
| -trans=mtransforms.ScaledTranslation(-20/72,7/72,fig.dpi_scale_trans) |
42 |
| -ax.text(0.0,1.0,label,transform=ax.transAxes+trans, |
43 |
| -fontsize='medium',va='bottom',fontfamily='serif') |
44 |
| - |
45 |
| -plt.show() |
| 41 | +# Put the label |
| 42 | +# - at the top left corner (axes fraction (0, 1)), |
| 43 | +# - offset 20 pixels left and 7 pixels up (offset points (-20, +7)), |
| 44 | +# i.e. just outside the axes. |
| 45 | +ax.annotate( |
| 46 | +label, |
| 47 | +xy=(0,1),xycoords='axes fraction', |
| 48 | +xytext=(-20,+7),textcoords='offset points', |
| 49 | +fontsize='medium',va='bottom',fontfamily='serif') |
46 | 50 |
|
47 | 51 | # %%
|
48 | 52 | # If we want it aligned with the title, either incorporate in the title or
|
49 | 53 | # use the *loc* keyword argument:
|
50 | 54 |
|
51 | 55 | fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
|
52 | 56 | layout='constrained')
|
53 |
| - |
54 | 57 | forlabel,axinaxs.items():
|
55 | 58 | ax.set_title('Normal Title',fontstyle='italic')
|
56 | 59 | ax.set_title(label,fontfamily='serif',loc='left',fontsize='medium')
|
|
67 | 70 | # - `matplotlib.figure.Figure.subplot_mosaic` /
|
68 | 71 | # `matplotlib.pyplot.subplot_mosaic`
|
69 | 72 | # - `matplotlib.axes.Axes.set_title`
|
70 |
| -# - `matplotlib.axes.Axes.text` |
71 |
| -# - `matplotlib.transforms.ScaledTranslation` |
| 73 | +# - `matplotlib.axes.Axes.annotate` |