Matplotlib 3次元の等高線図#
※記事内に商品プロモーションを含むことがあります。
公開日
この記事では、Matplotlibで3次元の等高線図を出力する方法を解説します。2次元の等高線図については以下の記事を参照ください。
3次元等高線図の基本#
plt.subplots()のsubplot_kwオプションに{'projection': '3d'}という辞書形式データを与えることで、3次元のプロットになります。等高線図とするには、さらにax.contour()またはax.contourf()メソッドを使います。前者は線のみ、後者は幅を持った等高線(塗り潰し)となります。
ax.contour(), ax.contourf()メソッドにはx, y, z座標を与えます。以下にax.contour()の例を示します。線の色は、z座標の値が大きいほど黄色に近くなり、小さいほど紫色に近くなります。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 2*np.pi, 0.1)
y = np.arange(0, 4*np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.sin(Y)
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.contour(X, Y, Z, levels=10)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
同じ数値を使ったax.contourf()の例を以下に示します。
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.contourf(X, Y, Z, levels=10)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
なお、変数X, Y, Zはいずれも同じ大きさの2次元配列となります。
print(X.shape)
print(Y.shape)
print(Z.shape)
(126, 63)
(126, 63)
(126, 63)
カラーマップの指定#
cmapオプションでカラーマップを指定します。
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.contour(X, Y, Z, levels=10, cmap="Reds")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
指定可能なカラーマップについては以下のページを参照下さい。
カラーバーの表示#
カラーバーを表示する場合、plt.colorbarを使用します。ax.pcolorの戻り値はPolyCollectionというクラスのオブジェクトです。これをplt.colorbarの最初の引数とします。また、axオプションにカラーバーを表示するグラフ(ここではax)を指定します。
fig, ax = plt.subplots(figsize=(8, 5), subplot_kw={'projection': '3d'})
mappable = ax.contour(X, Y, Z, levels=10, cmap="Reds")
plt.colorbar(mappable, ax=ax)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
fig.tight_layout()
plt.show()
表示するカラーマップの範囲を固定したい場合、最小値と最大値をそれぞれvmin, vmaxで指定します。
カラーバーにラベルを付ける場合、plt.colorbarの戻り値を取得(以下ではcbarとしています)し、この戻り値のset_labelメソッドを使用します。
fig, ax = plt.subplots(figsize=(8, 5), subplot_kw={'projection': '3d'})
mappable = ax.contour(X, Y, Z, levels=10, cmap="Reds")
cbar = plt.colorbar(mappable, ax=ax)
cbar.set_label('Value')
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
fig.tight_layout()
plt.show()
等高線の間隔#
等高線の間隔を指定する場合、表示する等高線の値を配列として、levelsオプションに与えます。また、levelsオプションにint型の数値を与えた場合、等高線の本数になります(間隔は自動的に決まります)。
fig, ax = plt.subplots(figsize=(8, 5), subplot_kw={'projection': '3d'})
mappable = ax.contour(X, Y, Z, levels=[-1.5, -1, -0.5, 0, 0.5, 1, 1.5])
plt.colorbar(mappable, ax=ax)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
fig.tight_layout()
plt.show()
fig, ax = plt.subplots(figsize=(8, 5), subplot_kw={'projection': '3d'})
mappable = ax.contour(X, Y, Z, levels=5)
plt.colorbar(mappable, ax=ax)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
線の太さ#
linewidthsオプションで線の太さを指定できます。
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.contour(X, Y, Z, levels=10, linewidths=4)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
透明度の指定#
等高線図の塗り潰し(ax.contourf)で透明度を指定するには、alphaオプションを使用します。0から1の範囲の値を取り、値が小さいほど透明になります(デフォルトは1)。以下にalpha=0.5とした例を示します。
fig, ax = plt.subplots(figsize=(8, 5), subplot_kw={'projection': '3d'})
mappable = ax.contourf(X, Y, Z, levels=10, alpha=0.5)
plt.colorbar(mappable, ax=ax)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
fig.tight_layout()
plt.show()