テキストの描画は Axes.text(x, y, text, ...)
を使う。
ボックス付きのテキストは属性を辞書形式で bbox パラメータに渡せばよい。
# sample code 5-1
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
ys = [5.4, 8.5, 12.8, 15.1, 19.5, 23.2, 24.3, 29.1, 24.2, 17.5, 14.0, 7.7]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.set_title('Average temperature')
ax.set_ylim(0, 35)
ax.plot(xs, ys)
ax.text(8, 30, 'peak', size=20, ha='center')
dic1={
'boxstyle': 'round',
'facecolor': 'pink',
'edgecolor': 'red',
'linestyle': 'solid',
'linewidth': 2
}
ax.text(2.5, 15, 'warmer and warmer', size=15, rotation=40, bbox=dic1)
dic2={
'boxstyle': 'circle',
'facecolor': 'cyan',
'edgecolor': 'blue',
'linestyle': 'dashed',
'linewidth': 2
}
ax.text(10, 20, 'getting chill', bbox=dic2)
plt.show()
マーカをプロットした散布図上に、マーカ毎の値をラベルとして表示する。 Axesオブジェクトのオフセット情報を管理する Transform オブジェクトを利用して、オフセット情報を一括で設定すると便利。
# sample code 5-2
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import numpy as np
xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
ys = [5.4, 8.5, 12.8, 15.1, 19.5, 23.2, 24.3, 29.1, 24.2, 17.5, 14.0, 7.7]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
# get the copy of 'offset' of the current 'Transform'
trans_offset = mtransforms.offset_copy(
ax.transData, # Transform of ax
fig=fig, # Fugure in which ax is deployed
x=0.05, # x offset
y=0.03, # y offset
units='inches' # unit of offset
)
for x, y in zip(xs, ys):
ax.plot(x, y, 'ro') # red circle
ax.text(
x,
y,
f'{y:.1f}',
transform=trans_offset, # changed offset
horizontalalignment='left', # horizontal basepoint
verticalalignment='bottom' # vertical basepoint
)
plt.show()
グラフ上の任意の位置に矢印付きのテキストを描画できる。
# sample code 5-3
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import numpy as np
xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
ys = [5.4, 8.5, 12.8, 15.1, 19.5, 23.2, 24.3, 29.1, 24.2, 17.5, 14.0, 7.7]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(xs, ys)
arrow1 = {
'facecolor': 'red',
'width': 1,
'headwidth': 10,
'headlength': 15,
'linewidth': 1,
'edgecolor': 'black',
'shrink': 0.05
}
ax.annotate('max point',
xy=(8, 29.1),
xytext=(9, 29),
arrowprops=arrow1,
fontsize=16)
plt.show()
# sample code 5-4
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import numpy as np
xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
ys = [5.4, 8.5, 12.8, 15.1, 19.5, 23.2, 24.3, 29.1, 24.2, 17.5, 14.0, 7.7]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(xs, ys)
ax.text(
8.3, # left point of text
29.1, # baseline of text,
'max point',
size=12,
rotation=10,
horizontalalignment='left',
verticalalignment='bottom',
bbox={
'boxstyle': 'larrow',
'facecolor': 'pink',
'edgecolor': 'red',
'linewidth': 1
}
)
plt.show()
数式は、文字rに続く文字列中の$のペアの中に数式要素を記述する。
# sample code 5-5-1
# Greek letters
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
cs=[
# 小文字
r'$\alpha$',
r'$\beta$',
r'$\chi$',
r'$\delta$',
r'$\digamma$',
r'$\epsilon$',
r'$\eta$',
r'$\gamma$',
r'$\iota$',
r'$\kappa$',
r'$\lambda$',
r'$\mu$',
r'$\nu$',
r'$\omega$',
r'$\phi$',
r'$\pi$',
r'$\psi$',
r'$\rho$',
r'$\sigma$',
r'$\tau$',
r'$\theta$',
r'$\upsilon$',
r'$\varepsilon$',
r'$\varkappa$',
r'$\varphi$',
r'$\varpi$',
r'$\varrho$',
r'$\varsigma$',
r'$\vartheta$',
r'$\xi$',
r'$\zeta$',
# 大文字
r'$\Delta$',
r'$\Gamma$',
r'$\Lambda$',
r'$\Omega$',
r'$\Pi$',
r'$\Psi$',
r'$\Sigma$'
]
N = len(cs)
H = 0.5 * N
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = [ 'DejaVu Sans' ]
plt.rcParams['mathtext.fontset'] = 'cm'
fig, ax = plt.subplots(1, 1, figsize=(2, H))
ax.set_xlim(0,2)
ax.set_ylim(0, H)
for i in range(N):
y = H - 0.5 * (i + 1) + 0.1
ax.text(0.2, y, cs[i], fontsize=20)
plt.show()
# sample code 5-5-2
# Accent sign
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
cs=[
r'$\acute{x}$',
r'$\bar{x}$',
r'$\breve{x}$',
r'$\ddot{x}$',
r'$\dot{x}$',
r'$\grave{x}$',
r'$\hat{x}$',
r'$\tilde{x}$',
r'$\vec{x}$',
r'$\overline{xyz}$'
]
N = len(cs)
H = 0.5 * N
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = [ 'DejaVu Sans' ]
plt.rcParams['mathtext.fontset'] = 'cm'
fig, ax = plt.subplots(1, 1, figsize=(2, 8))
ax.set_xlim(0,2)
ax.set_ylim(0, H)
for i in range(N):
y = H - 0.5 * (i + 1) + 0.1
ax.text(0.2, y, cs[i], fontsize=20)
plt.show()
# sample code 5-5-3
# Formula
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
cs=[
r'$\sqrt{2}$',
r'$\sqrt[3]{x}$',
r'$\frac{1}{2}-\frac{1}{3}=\frac{1}{6}$',
r'$\frac{3}{4} \binom{3}{4}$',
r'$\frac{5 - \frac{1}{x}}{4}$',
r'$(\frac{5 - \frac{1}{x}}{4})$',
r'$\left(\frac{5 - \frac{1}{x}}{4}\right)$',
r'$\alpha_i + \beta^i$',
r'$\sum_{i=0}^\infty x_i$'
]
N = len(cs)
H = 0.5 * N
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = [ 'DejaVu Sans' ]
plt.rcParams['mathtext.fontset'] = 'cm'
fig, ax = plt.subplots(1, 1, figsize=(2, 8))
ax.set_xlim(0,2)
ax.set_ylim(0, H)
for i in range(N):
y = H - 0.5 * (i + 1) + 0.2
ax.text(0.2, y, cs[i], fontsize=20)
plt.show()