import matplotlib.pyplot as plt
# リスト4.2.1:インライン表示で描画
fig = plt.figure()
ax = fig.add_subplot(111)
dat = [0, 1]
ax.plot(dat)
# 図をインライン表示
plt.show()
# リスト4.2.2:add_subplotでサブプロットを配置
# フィギュアの生成
fig = plt.figure()
# フィギュア内にサブプロットを3つ配置します
ax1 = fig.add_subplot(221) # 2行2列の1番
ax2 = fig.add_subplot(222) # 2行2列の2番
ax3 = fig.add_subplot(223) # 2行2列の3番
plt.show()
# リスト4.2.3:サブプロット番号割り当て順の確認
fig = plt.figure()
# サブプロットの作成
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
# 番号記述
for i, ax in enumerate([ax1, ax2, ax3], start=1):
txt = "ax{0}\n(22{0})".format(i)
ax.text(0.2, 0.4, txt, fontsize=24)
plt.show()
# リスト4.2.4:add_subplot()メソッドによるサブプロットの配置 (カンマ区切り記法)
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
# リスト4.2.5:subplots()によるサブプロットの配置
# フィギュアオブジェクトの作成とサブプロットの配置を同時に行う
fig, axes = plt.subplots(2, 2)
print(type(axes), axes)
plt.show()
<class 'numpy.ndarray'> [[<matplotlib.axes._subplots.AxesSubplot object at 0x0000027280DF8390> <matplotlib.axes._subplots.AxesSubplot object at 0x0000027280F27E10>] [<matplotlib.axes._subplots.AxesSubplot object at 0x0000027280EC1550> <matplotlib.axes._subplots.AxesSubplot object at 0x0000027280FF1828>]]
# リスト4.2.7:行列でサブプロットの位置を指定
# 1行2列目のサブプロットにサブプロットタイトルを指定
fig, axes = plt.subplots(2, 2)
axes[0, 1].set_title("Subplot 0-1")
plt.show()
# リスト4.2.8:ggplotスタイルで描画
# スタイルの適用
plt.style.use("ggplot")
fig = plt.figure()
ax = fig.add_subplot(111)
dat = [0, 1]
ax.plot(dat)
plt.show()