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/c9705902fccbba2c945ee944128a8922af039006bc984d23d51530339cef0a61.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/6f8d99eede1c0a61053f00c2dbf75fb7d1c757bae56f1d5310c209798d4456b9.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/97b69e0c65dee6432ec242b410ae38bdef6ffb67b7ae65c74ad7aba885ee64d5.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/d6a6581e661b12638b9ce3326f9daf4a592c6691330c531aee080f5e12d92f12.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/b723cffdb7b271a139e11e8ce6f5670f9364ed3b3c19286adce26218e725b687.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/32093c56d94ab211e4b072134ce92c8c9ae7e54607adfa2b68f0d6928698b276.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/a50807c4dd82278c4d2e2210131114ae6e9ac9b5ffd5121305ec8fc1330c7de4.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/497241a3f98f80b4b5ba27577b62929daf6e88b86379ed1fb8bb9d850157c0fc.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/33165ac2ec9cdab77c2a45be2a387e253077b3fc5db242e54630b4b11855ac7a.png