Matplotlibの極座標系の図#

公開日

極座標系の図をプロットするには、plt.subplots()subplot_kwオプションに{'projection': 'polar'}という辞書形式で与えます。

このとき、ax.plot()の最初の引数に角度[rad], 2番目の引数に原点からの距離を与えます。

import numpy as np
import matplotlib.pyplot as plt

theta = np.pi*np.arange(0, 3, 0.01)
r = np.arange(0, 3, 0.01)

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
plt.show()
../_images/5f9099c52f1d7d6bffaea4addd641860d7ac1ffc56e2f53701e763ee09986ea3.png

軸の範囲#

角度方向の軸の範囲はax.set_thetalim(), 動径方向の軸の範囲はax.set_rlim()でそれぞれ設定します。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_thetalim(0, np.pi)
ax.set_rlim(-3, 3)
plt.show()
../_images/ebe36e7cfc883517dad35c2751d6d5308be9897fb59d80d71a6997ac83d190e0.png

軸の目盛り#

偏角方向#

偏角方向の軸の目盛りを設定するには、ax.set_thetagrids()を使用します。表示したい目盛りの角度[deg]を配列で与えます。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_thetagrids([0, 90, 180, 270])
plt.show()
../_images/3c1ce58d0efe1ec8b6d7eace3b94acdc540ed87f49ceff127c0c3bef7aa5e027.png

labelsオプションで任意の目盛りラベルを設定できます。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_thetagrids([0, 90, 180, 270], labels=["E", "N", "W", "S"])
plt.show()
../_images/36bbd04f3d37514914b4feea6cdded35fb1ed04de8420a443fc68aa950be3589.png

動径方向#

動径方向の軸の目盛りを設定するには、ax.set_rgrids()を使用します。表示したい目盛りの位置を配列で与えます。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_rgrids([0, 1, 2, 3])
plt.show()
../_images/d199552cba7cdda8fbd726555baa3c538bcd7e5eefea0299e45a313d33dfe5bc.png

さらに、labelsオプションで任意の目盛りラベルを、angleで目盛りを表示する角度[deg]をそれぞれ設定できます。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_rgrids([0, 1, 2, 3], labels=["a", "b", "c", "d"], angle=60)
plt.show()
../_images/d8b30a9eef95ae2472c95b23b031564353c3a6902c85312166533d0eb2deaaac.png

偏角方向の軸の始点#

ax.set_theta_zero_location()によって、角度方向の軸の始点を指定できます。引数は方角であり、"N", "NW", "W", "SW", "S", "SE", "E", "NE"のいずれかを与えます。さらにoffsetオプションで半時計回りの回転角度[deg]を指定します(角度方向の軸の向きによらず、常に半時計回りの回転角度になります)。

以下は"N"で真上を始点とした例です。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_theta_zero_location("N")
plt.show()
../_images/f406e0d350988eec1038571752f358b380995f676cdc19bac35a21bddd34eaac.png

始点を真上からさらに半時計回りに10度回転させた例を以下に示します。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_theta_zero_location("N", offset=10)
plt.show()
../_images/7277faeee4a3fa80c2f9b1b8190774349d3910ea7d46218a7b02d373fbf2ab79.png

動径方向の軸の始点#

ax.set_rorigin()に負の値を指定することで、動径方向の原点を外側にずらせます。つまり、ドーナツグラフのような見た目になります。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_rorigin(-1)
plt.show()
../_images/fca511597de33580067721ce45537335354031128ad77dfa89e04e10d8bbd8ac.png

偏角の回転方向#

偏角の回転方向を反対(時計回り)にするには、ax.set_theta_direction(-1)を与えます。引数を1にすると、通常通り半時計回りとなります。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_theta_direction(-1)
plt.show()
../_images/61180a70e3d258f1739fe5f1e513676d53553c11250a390064bf274b8d330523.png

極座標系の様々なプロット#

棒グラフ#

極座標系の棒グラフをプロットするには、ax.bar()を用います。widthオプションで棒の幅、alphaオプションで棒の色の透明度を指定できます。

theta2 = np.pi*np.arange(0, 2, 0.5)
r2 = np.arange(1, 3, 0.5)

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.bar(theta2, r2, width=0.3, alpha=0.7)
ax.bar(theta2+0.2, r2+0.5, width=0.3, alpha=0.7)
plt.show()
../_images/9617485b3f1fd7cd2825042157c48ae9efbb34d5bca957942975feb928902d0c.png

散布図#

極座標系の散布図をプロットするには、ax.scatter()を用います。マーカーの大きさはsオプションで指定できます。その他のオプションについては散布図を参照して下さい。

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.scatter(theta2, r2)
ax.scatter(theta2+0.2, r2+0.5, s=100)
plt.show()
../_images/fbbe9d39b60b0efc4bc46addc5a382fcc920f9e0b66581e3768eb8b66d15bda2.png

ヒートマップ#

極座標系のヒートマップをプロットするには、ax.pcolor()を用います。最初の引数は角度[rad], 2番目の引数は動径、3番目の引数は色に対応するデータ(動径データ数×角度データ数の2次元配列)とします。

theta3 = np.linspace(0, 2*np.pi, 30) # データ数30の1次元配列(角度)
r3 = np.linspace(0, 100, 10) # データ数10の1次元配列(動径)

C = np.random.random_sample((10, 30)) # 10x30の2次元配列

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
mappable = ax.pcolor(theta3, r3, C, shading="auto")
plt.colorbar(mappable, ax=ax)
plt.show()
../_images/2061e869ef8d4d9a9ca7d5f46ff8b5a40909e9b8b5254e52c986add27106aed1.png

ax.pcolor()cmapオプションでカラーマップを指定できます。指定可能なカラーマップについては、以下のページも参照下さい。

Matplotlibのカラーマップ

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
mappable = ax.pcolor(theta3, r3, C, shading="auto", cmap="Blues")
plt.colorbar(mappable, ax=ax)
plt.show()
../_images/06be20dfeaa8bb3feab53dbf170ef950308a0437b7c03a5e96b525b84559bd5c.png

参考#

Polar plot — Matplotlib documentation