目次
グラフにテキストを追加する (1)
グラフにテキストを追加するためには、
matplotlib.pyplot モジュールの text 関数を使います。
ここで、text 関数の必須な引数は x, y, s の 3 つです。
- x: x座標
- y: y座標
- s: 表示したいテキスト
オプション引数については、
fontdict,
withdash
が有ります。
fontdict: フォントのプロパティを辞書型にしたものwithdash: 非推奨のパラメータ
更に可変長キーワード引数については、
color,
fontfamily,
fontweight,
fontstyle,
fontsize,
verticalalignment,
horizontalalignment,
bbox,
backgroundcolor,
path_effects
などが有ります。
color: 文字の色fontfamily(family): フォントファミリーfontweight(weight): 文字の太さfontstyle(style): 文字のスタイルfontsize(size): 文字の大きさverticalalignment(va): 垂直方向の配置方法horizontalalignment(ha): 水平方向の配置方法bbox: 境界ボックスのプロパティを辞書型にしたものbackgroundcolor: 背景の塗りつぶしの色path_effects:AbstractPathEffectオブジェクトのリスト
Example
source code
from matplotlib import pyplot
x = 0.4
y = 0.6
s = "Hello, world"
pyplot.text(x, y, s)
pyplot.grid()
pyplot.savefig("add_text.png")
result
グラフにテキストを追加する (2)
Axes オブジェクトの text メソッドを使うことでも、
グラフにテキストを追加することができます。
Example
source code
from matplotlib import pyplot
x = 0.4
y = 0.6
s = "Hello, world"
fig = pyplot.figure()
ax = fig.add_subplot(1, 1, 1)
ax.text(x, y, s)
ax.grid()
pyplot.savefig("add_text.png")
result
「グラフにテキストを追加する (1)」と同様。
フォントを変更する
テキストのフォントを変更したい場合は、
text メソッドの fontdict 引数に、
フォントのプロパティを辞書型にしたものを指定します。
また、
fontdict ではなく、
color,
fontfamily (family),
fontweight (weight),
fontstyle (style),
fontsize (size)
引数に任意の値を指定することでも、フォントを変更することができます。
Example
source code
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1, 1, 1)
font_dict = dict(style="italic",
size=16)
ax.text(0.2, 0.8, "Armadillo", fontdict=font_dict)
font_dict = dict(family="serif",
color="#ff0000",
weight="bold",
size=16)
ax.text(0.4, 0.6, "Bear", fontdict=font_dict)
ax.text(0.6, 0.4, "Cat", color="#0000ff",
fontfamily="serif",
fontweight="bold",
fontstyle="italic",
fontsize=16)
ax.grid()
pyplot.savefig("add_text_font.png")
result
配置方法を変更する
垂直方向の配置方法を変更したい場合は、
text メソッドの verticalalignment (va) に
"center",
"top",
"bottom",
"baseline",
"center_baseline",
のいずれかを指定します (デフォルト値は "baseline")。
水平方向の配置方法を変更したい場合は、
text メソッドの horizontalalignment (ha) に
"center",
"right",
"left",
のいずれかを指定します (デフォルト値は "left")。
Example
source code
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1, 1, 1)
ax.text(0.2, 0.8, "Egg-1", fontsize=16)
ax.text(0.4, 0.6, "Egg-2", fontsize=16,
verticalalignment="top")
ax.text(0.6, 0.4, "Egg-3", fontsize=16,
verticalalignment="bottom",
horizontalalignment="center")
ax.text(0.8, 0.2, "Egg-4", fontsize=16,
verticalalignment="center",
horizontalalignment="right")
ax.text(0.8, 0.2, "Egg-5", fontsize=16,
verticalalignment="center_baseline")
ax.grid()
pyplot.savefig("add_text_alignment.png")
result
罫線と背景を設定する
テキストの周囲に罫線を引いたり、背景を塗りつぶしたい場合は、
text メソッドの bbox 引数に、
matplotlib.patches.FancyBboxPatch クラス用のプロパティを辞書型にして指定します。
また、
bbox ではなく backgroundcolor に色を指定することでも、
背景を塗りつぶすことが可能です。
Example
source code
from matplotlib import pyplot
fig = pyplot.figure()
ax = fig.add_subplot(1, 1, 1)
bbox_dict = dict(edgecolor="#ff0000",
fill=False)
ax.text(0.2, 0.8, "Armadillo", bbox=bbox_dict)
bbox_dict = dict(boxstyle="round",
facecolor="#ffff00",
edgecolor="#0000ff",
linewidth=2.5,
linestyle="--")
ax.text(0.4, 0.6, "Bear", bbox=bbox_dict)
bbox_dict = dict(facecolor="#00ffff",
edgecolor="#009900",
alpha=0.25,
linewidth=2.5,
linestyle="-")
ax.text(0.6, 0.4, "Cat", bbox=bbox_dict)
ax.text(0.8, 0.2, "Dog", backgroundcolor="#ff9999")
ax.grid()
pyplot.savefig("add_text_fill_border.png")
result
テキストを縁取りする
テキストを縁取りしたい場合は、
text メソッドの
path_effects に
matplotlib.patheffects.withStroke オブジェクトをリストに追加したものを指定します。
Example
source code
from matplotlib import pyplot
from matplotlib.patheffects import withStroke
fig = pyplot.figure()
ax = fig.add_subplot(1, 1, 1)
effects=[withStroke(linewidth=2.5,
foreground="#0066ff")]
ax.text(0.2, 0.6, "Hello, world", color="#ffff00",
fontsize=32,
path_effects=effects)
ax.grid()
pyplot.savefig("add_text_effects.png")
result
参考リンク
- matplotlib.pyplot.text — Matplotlib 3.1.1 documentation
- matplotlib.axes.Axes.text — Matplotlib 3.1.1 documentation
- matplotlib.text — Matplotlib 3.1.1 documentation # Text
- matplotlib.patches.FancyBboxPatch — Matplotlib 3.1.1 documentation
- Controlling style of text and labels using a dictionary — Matplotlib 3.1.1 documentation
- Annotate Text Arrow — Matplotlib 3.1.1 documentation
- Patheffect Demo — Matplotlib 3.1.1 documentation