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/ae851eb1cf4c5bf3b3cabaca7ea84e1602451568b12591b933606cf1b5b005dc.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/a1a986027d87bc941fff853aaa8ec1ee35dbad6386033a318887fbd3f8e56633.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/a317629ab52c62b5a940a2b19e38e463283f30d5598d3c02fb82e5382ff4d539.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/edef2e47bf2e7ef97c3afd133198e24b7a3f214489991750642295b5e0c342cb.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/51d2dee933cef87e257bf095b5267b87f2f6f6a773d162bfab0c16d25408f52b.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/fb0937384d53569190c001ba5afdac5845b22de2a5ab480dc5912ed627e6b0a5.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/460976297270b398a5d39a7e9b0f1c093addc0841a92874912aac4cbde32256e.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/2547495da001f1406c357631078c5f8c7cd1c817d47e3fac2dd4f65fdf821230.png

動径方向の軸の始点#

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

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_rorigin(-1)
plt.show()
../_images/51efc5c5317a0c9500582fd1cd5bc4726406099aad69af25c36d0e2a327b4dac.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/245155d3bc843eac7dfdc1a75e4bb66c4a0e610968b7046f10d1eb16fd496083.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/80e0af48bfec4436416e1f1ef63dedb9bfd208ea5d3b80f53065d24452fe82df.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/d061027dfa7b423f46ef5af3fa482d7da5663d58d43e8de198fa09aaab0bdb8c.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/57e7567c62efe6c95b230d8d4aea0f9f30a514078cba2d23c3ed1023c3ecb6a2.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/84af3e19420aa0ff3e1754b04ab8b095faf219169760133d6da1405563e5a9de.png

参考#

Polar plot — Matplotlib documentation