Matplotlib 3次元の散布図#

※記事内に商品プロモーションを含むことがあります。

公開日

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

Matplotlibの散布図

3次元散布図の基本#

plt.subplots()subplot_kwオプションに{'projection': '3d'}という辞書形式データを与えることで、3次元のプロットになります。散布図とするには、さらにax.scatter()メソッドを使います。ax.scatter()に点のx, y, z座標を配列で与えます。以下に2点をプロットした例を示します。

import matplotlib.pyplot as plt

x = [0, 1]
y = [0, 1]
z = [0, 1]

fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.scatter(x, y, z)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
../_images/a1ac4da4605863d00228852644dbeebf4d0986816455c435ae227fae3a8c47af.png

Matplotlibの公式サイトによると、以下のようにplt.figure()fig.add_subplot()を組み合わせた方法が主流のようです。しかし、このサイトでは2次元のグラフと形式を揃えるため、基本的に上の記述方法で解説します。

fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(projection='3d')
ax.scatter(x, y, z)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
../_images/a1ac4da4605863d00228852644dbeebf4d0986816455c435ae227fae3a8c47af.png

なお、projection='3d'とした場合、axAxes3DSubplotオブジェクトとなります。

print(type(ax))
<class 'mpl_toolkits.mplot3d.axes3d.Axes3D'>

マーカーの大きさ・色・種類#

マーカーの大きさはsオプションで指定します。

fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.scatter(x, y, z, s=200)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
../_images/8221b9ff86d96d4449809235d873afddfd8d8c416c9a23a496207e34a2f5641a.png

マーカーの色はcオプションで指定します。cオプションの詳細は以下の記事を参考にして下さい。

Matplotlib 色の書式

fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.scatter(x, y, z, c="red")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
../_images/e595a9ec6ddc0c56c8b6e4af8d532d02109f9afdf74457666da8122f3bf19990.png

また、alphaオプションで透明度を変更できます。0から1の範囲を取り、値が小さいほど透明に近づきます。

fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.scatter(x, y, z, alpha=0.5)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
../_images/4f5da6197c5639b20da11ee060d24399f0f18d9f58d6d2e9ae018011cbaa75d6.png

マーカーの種類はmarkerオプションで指定します。

fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.scatter(x, y, [1.0, 1.0], s=100, marker="o")
ax.scatter(x, y, [0.8, 0.8], s=100, marker="v")
ax.scatter(x, y, [0.6, 0.6], s=100, marker="s")
ax.scatter(x, y, [0.4, 0.4], s=100, marker="o")
ax.scatter(x, y, [0.2, 0.2], s=100, marker="D")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
../_images/8bc1d912722a8c5712987aced92c82335b3e991358dde7ccec2554a73f1ad30a.png

カラーマップの指定#

散布図の各点の色を、値に合わせて指定できます。cに値を、cmapにカラーマップを指定します。

以下の例では、Bluesというカラーマップを使用しています。c1の値が大きいほど、点の青色が濃くなっています。

x1 = range(10)
y1 = range(10)
z1 = range(10)
c1 = range(10)

fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'projection': '3d'})
ax.scatter(x1, y1, z1, c=c1, cmap="Blues")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
../_images/18ac97e9ca5a22f0bd077ad7664a2622ec372783a1b1dafdeba5e224b046a6d6.png

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

Matplotlibのカラーマップ

カラーバーを表示する場合、plt.colorbarを使用します。ax.scatterの戻り値はPathCollectionというクラスのオブジェクトです。これをplt.colorbarの最初の引数とします。また、axオプションにカラーバーを表示するグラフ(ここではax)を指定します。

fig, ax = plt.subplots(figsize=(7, 5), subplot_kw={'projection': '3d'})
mappable = ax.scatter(x1, y1, z1, c=c1, cmap="Blues")
plt.colorbar(mappable, ax=ax)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
fig.tight_layout()
plt.show()
../_images/f627081c1d0d4d6da2b07aa8cd54f468e7c2aba8d4165551d23917584094e4e8.png