import matplotlib.pyplot as plt
import numpy as np
# リスト5.6.1:軸の書式を設定せずに描画 (デフォルト)
x1 = np.arange(0, 15, 0.2)
x2 = np.arange(0, 20, 0.2)
y1 = np.sin(x1)
y2 = np.sin(x2) * 2
y3 = np.cos(x2) * 2
y4 = np.cos(x1)
fig = plt.figure(figsize=(3, 3))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.plot(x1, y1)
ax2.plot(x2, y2)
ax3.plot(x2, y3)
ax4.plot(x1, y4)
fig.subplots_adjust(wspace=0.35, hspace=0.35)
plt.show()
# リスト5.6.2:軸の範囲設定
fig = plt.figure(figsize=(3, 3))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.plot(x1, y1, label="y1")
ax2.plot(x2, y2, label="y2")
ax3.plot(x2, y3, label="y3")
ax4.plot(x1, y4, label="y4")
# 軸範囲の設定
axes = [ax1, ax2, ax3, ax4]
for ax in axes:
ax.set_xlim(0, 20)
ax.set_ylim(-2, 2)
fig.subplots_adjust(wspace=0.35, hspace=0.35, bottom=0.2)
plt.show()
# リスト5.6.3:subplots関数による描画
# データセットの作成
x1 = np.arange(0, 15, 0.2)
x2 = np.arange(0, 20, 0.2)
y1 = np.sin(x1)
y2 = np.sin(x2) * 2
y3 = np.cos(x2) * 2
y4 = np.cos(x1)
# フィギュアオブジェクトの生成
fig, axes = plt.subplots(2, 2, figsize=(3, 3), sharex=True, sharey=True)
# 描画
axes[0, 0].plot(x1, y1)
axes[0, 1].plot(x2, y2)
axes[1, 0].plot(x2, y3)
axes[1, 1].plot(x1, y4)
plt.show()
# リスト5.6.4:対数軸の設定
x1 = np.linspace(0, 100, 100)
x2 = np.linspace(0, 1000000, 100)
y1 = np.linspace(0, 1000000, 100)
y2 = np.linspace(0, 100, 100)
fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.plot(x1, y1)
ax2.plot(x2, y2)
# 軸を対数に設定
ax1.set_yscale("log")
ax2.set_xscale("log")
ax1.set_title("Y軸が対数軸")
ax2.set_title("X軸が対数軸")
plt.show()
# リスト5.6.5:2軸の設定
# データセットの作成
x1 = np.linspace(0, 10, 100)
x2 = np.linspace(0, 100, 100)
y1 = np.linspace(0, 100, 100)
y2 = np.linspace(0, 10, 100)
fig = plt.figure(figsize=(9, 3))
ax1_1 = fig.add_subplot(121)
ax2_1 = fig.add_subplot(122)
ax1_1.plot(x1, y1)
ax1_2 = ax1_1.twinx()
ax1_2.plot(x1, y2)
ax2_1.plot(x1, y1)
ax2_2 = ax2_1.twiny()
ax2_2.plot(x2, y1)
ax1_1.set_title("2つのY軸", y=0.8)
ax2_1.set_title("2つのX軸", y=0.8)
plt.show()
# リスト5.6.6:軸ラベルの設定
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 15, 0.2)
y = np.sin(x)
fig = plt.figure(figsize=(4, 2))
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.set_xlim([0, 15])
ax.set_ylim([-1, 1])
# 軸ラベルの設定
ax.set_xlabel("x", fontsize=20, color="red")
ax.set_ylabel("y", {"fontsize": 20, "color": "green"})
fig.subplots_adjust(bottom=0.3, left=0.2)
plt.show()
# リスト5.6.7:主目盛りと主目盛りラベルを設定
fig = plt.figure(figsize=(4, 2))
ax = fig.add_subplot(111)
ax.plot(x, y)
# 軸の範囲の設定
ax.set_xlim([0, 15])
ax.set_ylim([-1, 1])
# 主目盛りの設定
ax.set_xticks([0, 5, 10, 15])
ax.set_yticks([-1, 0, 1])
# 主目盛りラベルの設定
ax.set_xticklabels(["zero", "five", 10, 15])
ax.set_yticklabels([])
fig.subplots_adjust(bottom=0.3, left=0.2)
plt.show()
# リスト5.6.8:目盛りの書式設定
fig = plt.figure(figsize=(4, 4))
ax = fig.add_subplot(111)
# 補助目盛りの描画
ax.minorticks_on()
# X軸の補助目盛りを設定
ax.tick_params(axis="x", which="minor", bottom="off")
# X軸の主目盛りを設定
ax.tick_params(
axis="x", which="major", direction="inout", length=10, labelcolor="green"
)
# Y軸の補助目盛りを設定
ax.tick_params(axis="y", which="minor", direction="out", length=5, color="red")
# Y軸の主目盛りを設定
ax.tick_params(axis="y", which="major", direction="inout", length=10, color="blue")
plt.show()
# リスト5.7.5:罫線の描画
fig = plt.figure(figsize=(4, 4))
ax = fig.add_subplot(111)
# 補助目盛りの描画
ax.minorticks_on()
# 罫線の描画
ax.grid(which="major", axis="y", color="blue", linestyle="-", linewidth=1)
ax.grid(which="minor", axis="y", color="grey", linestyle="--", linewidth=1)
plt.show()