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()
![../_images/9ef12642e40781505e40d6763fd2e4a609d83f2656d625b5471deb5975f8dc7a.png](../_images/9ef12642e40781505e40d6763fd2e4a609d83f2656d625b5471deb5975f8dc7a.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/138bd163c64c9e20ba0b2cdcbda0d3f1ec9a06aa65f7ddf0363bd3b289538260.png](../_images/138bd163c64c9e20ba0b2cdcbda0d3f1ec9a06aa65f7ddf0363bd3b289538260.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/8a7007cbeb498cdbe56004b4ce243dc38c4998738b087f2bdc05595fbca8a6de.png](../_images/8a7007cbeb498cdbe56004b4ce243dc38c4998738b087f2bdc05595fbca8a6de.png)
指定可能なカラーマップについては以下のページを参照下さい。
カラーバーの表示#
カラーバーを表示する場合、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/5ef7672a18af78bd5672ea3a64cee037e6cf55440f972c6e6dddcb24735cae39.png](../_images/5ef7672a18af78bd5672ea3a64cee037e6cf55440f972c6e6dddcb24735cae39.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/373597a0821e2b3f26f79d4392d9ecc6e80a457e0c7414a402c2ecd350760f06.png](../_images/373597a0821e2b3f26f79d4392d9ecc6e80a457e0c7414a402c2ecd350760f06.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/7b5e25514c6aa87e6d2c0e4b0541d60a9e058495ddbdef07946f236ca5c9b8f3.png](../_images/7b5e25514c6aa87e6d2c0e4b0541d60a9e058495ddbdef07946f236ca5c9b8f3.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/b7756c7c4ddfd221e8b1f6aae67792912c975ed87099a37ca0a2133c6e09bbbe.png](../_images/b7756c7c4ddfd221e8b1f6aae67792912c975ed87099a37ca0a2133c6e09bbbe.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/1103d4a9d87c72c02993da3dccfdbe1e5f9b056a7b1562b072867ae2cfa4fa6c.png](../_images/1103d4a9d87c72c02993da3dccfdbe1e5f9b056a7b1562b072867ae2cfa4fa6c.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/a376d8214c157633d7f42de475c796a8ff6fd778758e6a830dcd7f77919ed0c6.png](../_images/a376d8214c157633d7f42de475c796a8ff6fd778758e6a830dcd7f77919ed0c6.png)