Matplotlib 3次元の等高線図#

公開日

この記事では、Matplotlibで3次元の等高線図を出力する方法を解説します。2次元の等高線図については以下の記事を参照ください。

Matplotlibの等高線図

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()
../_images/afba26a4fa5370ea9880b998f6c9a43bd9febba3786076bcfbcc61909be92d25.png

同じ数値を使った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()
../_images/fb3aa3473742e66daa037b9fd8ab920b5137d97307cec343265fd95d38bd886d.png

なお、変数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()
../_images/4375b6c9192f989333be98b1ffc47ef755882c6d3f3cbd52101cd8a47ce38800.png

指定可能なカラーマップについては以下のページを参照下さい。

Matplotlibのカラーマップ

カラーバーの表示#

カラーバーを表示する場合、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()
../_images/7bd319e017e921409abf994eaf4290042aafba86c32804d21486d57778be4a72.png

表示するカラーマップの範囲を固定したい場合、最小値と最大値をそれぞれ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()
../_images/f1b1e0d05390f02b2b311ed89a0e97c9c1acd16b8e1ccb782c62482050b49dde.png

等高線の間隔#

等高線の間隔を指定する場合、表示する等高線の値を配列として、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()
../_images/816524ae8df4efa61209c8bfda90cd6563eab2aa52ca4cb0a86a64105de8044d.png
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()
../_images/5b8fc559d89e83274a910eb346107ae090a2329680288d57cf4048a592aba139.png

線の太さ#

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()
../_images/89339fa905f53daeb567c645ecdf77640c1732cabfd2474c9cd0af1f8b5b8c83.png

透明度の指定#

等高線図の塗り潰し(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()
../_images/089a05c6c4c12b45a55b8bcba13eb47e8964db0f7abc6f76649c43ad3edb410d.png