Matplotlibの散布図#

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

公開日

Matplotlibで散布図を出力するには、ax.scatterを使用します。ax.scatterの最初の引数に横軸方向の値、2番目の引数に縦軸方向の値をそれぞれ配列で与えます。

import numpy as np
import matplotlib.pyplot as plt

x = np.array([4, 6, 7, 3, 5]) # 横軸の値
y = np.array([5, 3, 7, 4, 6]) # 縦軸の値

fig, ax = plt.subplots()
ax.scatter(x, y)
plt.show()
../_images/2655df6a1d6eecb0304a1f3d846a75e4b9dd3f09162e076b47b7be057ea3b7b7.png

複数系列の散布図#

散布図に系列を追加したい場合、以下のようにax.plotを追加します。labelオプションとax.legend()を使うことで、系列名を凡例として表示できます。

x2 = np.array([1, 3, 2, 5, 6]) # 横軸の値
y2 = np.array([2, 5, 4, 7, 6]) # 縦軸の値

fig, ax = plt.subplots()
ax.scatter(x, y, label="y")
ax.scatter(x2, y2, label="y2")
ax.legend()
plt.show()
../_images/6cd8a2b129dc836359e3fc8094edf7721d38b939e67a6eb3c614eb7e0aba0e85.png

マーカーの大きさ・色・種類を変更する#

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

fig, ax = plt.subplots()
ax.scatter(x, y, s=200)
plt.show()
../_images/44746a4f4f15ac94003a08f7490b62a68f98e3905315ccbca184a14d8392a0ff.png

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

Matplotlib 色の書式

fig, ax = plt.subplots()
ax.scatter(x, y, c="green")
plt.show()
../_images/82f76b3887857070c19b6178e04ae8d4120b644758f14b51844a4f58524c2587.png

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

fig, ax = plt.subplots()
ax.scatter(x, y, alpha=0.5)
plt.show()
../_images/9cb43284279aa5b137b7161873aebc097915b1c08855d92222ea59e4f25e60c9.png

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

fig, ax = plt.subplots()
ax.scatter(x, y, marker="o")
ax.scatter(x, 0.8*y, marker="v")
ax.scatter(x, 0.6*y, marker="s")
ax.scatter(x, 0.4*y, marker="+")
ax.scatter(x, 0.2*y, marker="o")
ax.scatter(x, 0*y, marker="D")
plt.show()
../_images/669e68f649aab06b196fdf1e779ef85e306fa166f73dee0cf17f7e041e63729a.png

主なmarkerオプションを以下の表に示します。

marker

説明

o

v

下向き三角

^

上向き三角

<

左向き三角

>

右向き三角

s

四角形(square)

p

五角形(pentagon)

+

+記号

x

x記号

D

ダイヤモンド

その他に利用可能なマーカーの種類は、以下の公式ページを参照してください。

matplotlib.markers — Matplotlib documentation

マーカーの枠線の太さ・色などは変更できます。指定できるオプションを以下の表に示します。

オプション

説明

linewidths

マーカー枠線の太さ

edgecolors

マーカー枠線の色

これらのオプションを使用した例を以下に示します。マーカーの枠線の色はオレンジ、枠線の太さは3となります。

fig, ax = plt.subplots()
ax.scatter(x, y, s=200, edgecolors="orange", linewidths=3)
plt.show()
../_images/ae88b84ae11ca5a647d98a8c066c5a8c66a3cd2f8f2065b353f4e069035ee058.png

カラーマップの指定#

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

x1 = np.array([1, 2, 3, 4]) # 横軸の値
y1 = np.array([1, 2, 3, 4]) # 縦軸の値
v1 = np.array([1, 2, 3, 4]) # 点の色に対応する値

fig, ax = plt.subplots()
ax.scatter(x1, y1, c=v1, cmap="Reds")
plt.show()
../_images/6100ae3ba6cc00583d8fd204d24bc53aa308d520b171a00afaf4868fb4f4b96d.png

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

Matplotlibのカラーマップ

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

fig, ax = plt.subplots()
mappable = ax.scatter(x1, y1, c=v1, cmap="Reds")
plt.colorbar(mappable, ax=ax)
plt.show()
../_images/9e7ddc55b8ba8cd99d7081b0fa2ce5eb87ffeaab6b07a37807a744e8f8b40534.png

また、表示するカラーマップの範囲を固定したい場合、最小値と最大値をそれぞれvmin, vmaxで指定します。

fig, ax = plt.subplots()
mappable = ax.scatter(x1, y1, c=v1, cmap="Reds", vmin=-5, vmax=3)
plt.colorbar(mappable, ax=ax)
plt.show()
../_images/8b9114e33019c90d429c4e07e8eb773e41d24bd493f73f44944ad7f1265ffb21.png

以下にカラーバーにラベルを付けるコードを示します。plt.colorbarの戻り値はColorbarというクラスのオブジェクトです(以下ではcbarとしています)。このオブジェクトのset_labelメソッドを使用します。

fig, ax = plt.subplots()
mappable = ax.scatter(x1, y1, c=v1, cmap="Reds", vmin=-5, vmax=3)
cbar = plt.colorbar(mappable, ax=ax)
cbar.set_label('Value')
plt.show()
../_images/278075b7dd5397b52cc27e7cf3718cb0391e7cf436675cff4f2ec3d7f23aa67a.png