Plotly⑲(ポーラチャート)

ポーラチャート

Plotlyでポーラチャートを表示するにはScatterpolarクラスまたはBarpolarクラスを使用します。

データとして、引数 rに極座標の原点からの距離を設定し、引数 thetaには角度を度数法で設定します。

引数 thetaに離散値が設定された場合は、角度が等間隔に割り当てられます。

[Google Colaboratory]

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
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

np.random.seed(2)
r1 = np.random.rand(6) # 乱数生成
theta = np.linspace(0, 360, 7)[:-1]
r1_close = np.hstack([r1, np.array(r1[0])])
r2 = r1 + np.random.uniform(-0.3, 0.3, 6)
r2_close = np.hstack([r2, np.array(r2[0])])
label = list("ABCDEF")

polar_fig = make_subplots(
rows=2,
cols=2,
specs=[
[{"type": "polar"}, {"type": "polar"}],
[{"type": "polar"}, {"type": "polar"}]
]
)
# 点で描画
polar_fig.add_trace(
go.Scatterpolar(r=r1, theta=theta, mode="markers"), row=1, col=1
)
# 線で描画(レーダチャート)
polar_fig.add_trace(
go.Scatterpolar(
r=r1_close, theta=label, mode="lines", fill="toself", name="r1"
),
row=1,
col=2
)
polar_fig.add_trace(
go.Scatterpolar(
r=r2_close, theta=label, mode="lines", fill="toself", name="r2"
),
row=1,
col=2
)
# 鶏頭図
polar_fig.add_trace(go.Barpolar(r=r1, theta=label), row=2, col=1)
polar_fig.add_trace(go.Barpolar(r=r2, theta=label), row=2, col=1)
polar_fig.show()

[実行結果]

上図のそれぞれのサブプロットのグラフは下記の通りです。

  • 左上のグラフ
    散布図(点で描画)
  • 右上のグラフ
    レーダーチャート(線で描画)
  • 左下のグラフ
    鶏頭図(積み上げて描画)