Matplotlibの等高線図#
※記事内に商品プロモーションを含むことがあります。
公開日
Matplotlibで等高線図を出力するには、ax.contour
(線のみ)またはax.contourf
(塗り潰し)を使用します。
どちらの手法でも、最初の引数に2次元配列を与えます。このページでは前者のax.contour
を例としてオプションの指定方法を書いていますが、ax.contourf
も同様に指定できます。
import numpy as np
import matplotlib.pyplot as plt
C = np.arange(50).reshape(5, 10)
fig, ax = plt.subplots()
ax.contour(C) # 線のみ
plt.show()
fig, ax = plt.subplots()
ax.contourf(C) # 塗り潰し
plt.show()


上の等高線図では、値が小さい箇所は紫色、値が大きい箇所は緑色・黄色になります。
ここで、C
は以下のような5行10列の2次元配列です。等高線図の横軸は0から9, 縦軸は0から4の範囲で出力されています。
C
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]])
目盛りの指定#
等高線図の横幅と縦軸の目盛りを指定する場合、以下のようにax.pcolor
の1, 2番目の引数に配列として与えます。
X = np.arange(5,15) # 横幅の目盛り
Y = np.arange(5,10) # 縦幅の目盛り
fig, ax = plt.subplots()
ax.contour(X, Y, C)
plt.show()

カラーマップの指定#
cmap
オプションでカラーマップを指定します。
fig, ax = plt.subplots()
ax.contour(C, cmap="Reds")
plt.show()

指定可能なカラーマップについては、以下のページも参照下さい。
カラーバーを表示する場合、plt.colorbar
を使用します。ax.pcolor
の戻り値はPolyCollection
というクラスのオブジェクトです。これをplt.colorbar
の最初の引数とします。また、ax
オプションにカラーバーを表示するグラフ(ここではax
)を指定します。
fig, ax = plt.subplots()
mappable = ax.contour(C)
plt.colorbar(mappable, ax=ax)
plt.show()

表示するカラーマップの範囲を固定したい場合、最小値と最大値をそれぞれvmin
, vmax
で指定します。
カラーバーにラベルを付ける場合、plt.colorbar
の戻り値を取得(以下ではcbar
としています)し、この戻り値のset_label
メソッドを使用します。
fig, ax = plt.subplots()
mappable = ax.contour(C, vmin=10, vmax=40)
cbar = plt.colorbar(mappable, ax=ax)
cbar.set_label('Value')
plt.show()

等高線の間隔#
等高線の間隔を指定する場合、表示する等高線の値を配列として、levels
オプションに与えます。また、levels
オプションにint
型の数値を与えた場合、等高線の本数になります(間隔は自動的に決まります)。
fig, ax = plt.subplots()
mappable = ax.contour(C, levels=[0, 10, 20, 30, 40, 50])
plt.colorbar(mappable, ax=ax)
plt.show()

fig, ax = plt.subplots()
mappable = ax.contour(C, levels=6)
plt.colorbar(mappable, ax=ax)
plt.show()

等高線のラベル#
等高線の値をラベルとして表示するには、ax.contour()
の戻り値であるContourSet
クラスのオブジェクトを取得し、ax.clabel()
に渡します。
fig, ax = plt.subplots()
cs = ax.contour(C) # cs: ContourSetクラスのオブジェクト
ax.clabel(cs)
plt.show()

ax.clabel
の主なオプションを以下の表に示します。
オプション |
説明 |
---|---|
|
フォントのサイズ |
|
ラベルの色 |
|
|
|
|
|
ラベルのフォーマットを指定(例: |
ラベルのフォントサイズはfontsize
, 色はcolors
オプションでそれぞれ指定できます。
fig, ax = plt.subplots()
cs = ax.contour(C)
ax.clabel(cs, fontsize=15, colors="black")
plt.show()

また、inline=False
(文字列を等高線を重ねる)、 fmt='%.2f'
(小数点以下2桁)とした例を以下に示します。
fig, ax = plt.subplots()
cs = ax.contour(C) # cs: ContourSetクラスのオブジェクト
ax.clabel(cs, inline=False, fmt='%.2f')
plt.show()

等高線の色の上下限#
等高線の色の上限値と下限値は、それぞれvmax
, vmin
で指定します。vmax
以上の値とvmin
以下の値の色は、カラーマップの両端の色と同じになります。以下は0から49までの値をとる配列C
に対して、vmax=40, vmin=10
とした例です。10から40までの値の変化が分かりやすくなります。
fig, ax = plt.subplots()
mappable = ax.contour(C, vmax=40, vmin=10, levels=[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
plt.colorbar(mappable, ax=ax)
plt.show()

また、以下の例はvmax=70, vmin=-20
とした例です。こちらでは、値の変化が分かりづらくなります(値の変化が小さいことを示せます)。
fig, ax = plt.subplots()
mappable = ax.contour(C, vmax=70, vmin=-20, levels=[-20, -10, 0, 10, 20, 30, 40, 50, 60, 70])
plt.colorbar(mappable, ax=ax)
plt.show()

線の太さの変更#
linewidths
オプションで線の太さを指定できます。
fig, ax = plt.subplots()
ax.contour(C, linewidths=8)
plt.show()

透明度の指定#
等高線図の塗り潰し(ax.contourf
)で透明度を指定するには、alpha
オプションを使用します。0から1の範囲の値を取り、値が小さいほど透明になります(デフォルトは1)。
fig, ax = plt.subplots()
ax.contourf(C, alpha=1) # デフォルト
plt.show()
fig, ax = plt.subplots()
ax.contourf(C, alpha=0.5)
plt.show()

