Plotly⑳(三角図)

三角図

Plotlyで三角図を表示するにはScatterternaryクラスを使用します。

まず、election(選挙)データセットを読み込みます。

[Google Colaboratory]

1
2
3
4
import plotly

election = plotly.data.election()
election.head()

[実行結果]


読み込んだデータセットのBergeron列、Coderre列、Joly列を、Scatterternaryクラスa、b、cに設定し三角図を描画します。(4~6行目)

引数 modeには描画モードを設定します。(7行目)

また、三角図の軸のスタイルを設定するにはLayoutクラス引数 ternaryに辞書型データを設定します。(10~16行目)

[Google Colaboratory]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import plotly.graph_objects as go

scatterternary_trace = go.Scatterternary(
a=election["Bergeron"], # a軸のデータを設定
b=election["Coderre"], # b軸のデータを設定
c=election["Joly"], # c軸のデータを設定
mode="markers", # 描画モードを設定
marker={"size": election["total"] * 1e-3}
)
scatterternary_layout = go.Layout(
ternary={
"aaxis": {"title": "Bergeron"}, # a軸の名称を設定
"baxis": {"title": "Coderre"}, # b軸の名称を設定
"caxis": {"title": "Joly"} # c軸の名称を設定
}
)
go.Figure(scatterternary_trace, layout=scatterternary_layout)

[実行結果]