# リスト5.7.1:凡例の設定
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 20, 0.2)
y1 = np.sin(x)
y2 = np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
# 凡例表示
ax.legend()
plt.show()
<matplotlib.figure.Figure at 0x25b86b64cf8>
# リスト5.7.2:凡例をサブプロットの外側に配置
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
# 凡例表示 ・ 位置調整
ax.legend(loc="upper left", bbox_to_anchor=(1, 1))
# サブプロットの位置の調整
fig.subplots_adjust(left=0.2, bottom=0.2, right=0.75, top=0.8)
plt.show()
# リスト5.7.3:borderaxespadの設定
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
# 凡例表示 ・ 位置調整
ax.legend(loc="upper left", bbox_to_anchor=(1, 1), borderaxespad=0)
fig.subplots_adjust(left=0.2, bottom=0.2, right=0.75, top=0.8)
plt.show()
# リスト5.7.4:サブプロットタイトルの設定
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
# サブプロットタイトルの設定
ax.set_title("sin and cos")
plt.show()
# リスト5.7.5:フィギュアのタイトルの設定
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
ax.set_title("sin and cos")
# フィギュアのタイトルの設定
fig.suptitle("三角関数")
plt.show()
# リスト5.7.6:凡例とタイトルの書式設定
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
# フォントの書式辞書
fdict = {"family": "serif", "style": "italic", "weight": "bold", "size": 12}
# テキストボックスの書式辞書
bdict = {
"facecolor": "k",
"edgecolor": "None",
"alpha": 0.5,
"boxstyle": "rarrow",
"linewidth": 2,
}
ax.legend(loc="upper left", bbox_to_anchor=(1.02, 1), borderaxespad=0, prop=fdict)
# サブプロットタイトルの設定
ax.set_title("sin and cos", fontdict=fdict, bbox=bdict, color="white")
# フィギュアタイトルの設定
fig.suptitle("三角関数", y=0.95, size=16, color="grey", fontweight="bold")
fig.subplots_adjust(left=0.2, bottom=0.2, right=0.75, top=0.8)
plt.show()