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

Commitd48b2a8

Browse files
authored
Merge pull request#25905 from anntzer/ls
Use annotate coordinate systems to simplify label_subplots.
2 parents444d042 +9dbe278 commitd48b2a8

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

‎galleries/examples/text_labels_and_annotations/label_subplots.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,58 @@
33
Labelling subplots
44
==================
55
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.
88
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+
We showcase two methods to position text at a given physical offset (in
10+
fontsize units or in points) away from a corner of the Axes: one using
11+
`~.Axes.annotate`, and one using `.ScaledTranslation`.
12+
13+
For convenience, this example uses `.pyplot.subplot_mosaic` and subplot
14+
labels as keys for the subplots. However, the approach also works with
15+
`.pyplot.subplots` or keys that are different than what you want to label the
16+
subplot with.
1417
"""
1518

1619
importmatplotlib.pyplotasplt
1720

18-
importmatplotlib.transformsasmtransforms
21+
frommatplotlib.transformsimportScaledTranslation
1922

23+
# %%
2024
fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
2125
layout='constrained')
22-
2326
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()
27+
# Use Axes.annotate to put the label
28+
# - at the top left corner (axes fraction (0, 1)),
29+
# - offset half-a-fontsize right and half-a-fontsize down
30+
# (offset fontsize (+0.5, -0.5)),
31+
# i.e. just inside the axes.
32+
ax.annotate(
33+
label,
34+
xy=(0,1),xycoords='axes fraction',
35+
xytext=(+0.5,-0.5),textcoords='offset fontsize',
36+
fontsize='medium',verticalalignment='top',fontfamily='serif',
37+
bbox=dict(facecolor='0.7',edgecolor='none',pad=3.0))
3138

3239
# %%
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-
3640
fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
3741
layout='constrained')
38-
3942
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()
43+
# Use ScaledTranslation to put the label
44+
# - at the top left corner (axes fraction (0, 1)),
45+
# - offset 20 pixels left and 7 pixels up (offset points (-20, +7)),
46+
# i.e. just outside the axes.
47+
ax.text(
48+
0.0,1.0,label,transform=(
49+
ax.transAxes+ScaledTranslation(-20/72,+7/72,fig.dpi_scale_trans)),
50+
fontsize='medium',va='bottom',fontfamily='serif')
4651

4752
# %%
4853
# If we want it aligned with the title, either incorporate in the title or
4954
# use the *loc* keyword argument:
5055

5156
fig,axs=plt.subplot_mosaic([['a)','c)'], ['b)','c)'], ['d)','d)']],
5257
layout='constrained')
53-
5458
forlabel,axinaxs.items():
5559
ax.set_title('Normal Title',fontstyle='italic')
5660
ax.set_title(label,fontfamily='serif',loc='left',fontsize='medium')
@@ -67,5 +71,4 @@
6771
# - `matplotlib.figure.Figure.subplot_mosaic` /
6872
# `matplotlib.pyplot.subplot_mosaic`
6973
# - `matplotlib.axes.Axes.set_title`
70-
# - `matplotlib.axes.Axes.text`
71-
# - `matplotlib.transforms.ScaledTranslation`
74+
# - `matplotlib.axes.Axes.annotate`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp