Plotly ボタンウィジェット#
※記事内に商品プロモーションを含むことがあります。
公開日
Pythonのインタラクティブなグラフを描画できるライブラリPlotlyにて、ボタンウィジェットを追加する方法を解説します。
Plotlyには、高水準のAPIであるPlotly Expressと呼ばれるものもありますが、この記事では細かい調節ができるgraph_objects
を対象としています。
リンク
ボタンウィジェットの基礎#
Plotlyのヒートマップ go.Heatmap
に、カラーマップを切り替えるボタンを追加する例を示します。
import plotly.graph_objects as go
z_data = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
fig = go.Figure(
data=go.Heatmap(z=z_data, colorscale="Viridis")
)
buttons = [
dict(
args=["colorscale", "Viridis"],
label="Viridis",
method="restyle",
),
dict(
args=["colorscale", "Jet"],
label="Jet",
method="restyle",
),
dict(
args=["colorscale", "Reds"],
label="Reds",
method="restyle",
),
]
fig.update_layout(
updatemenus=[
dict(
type="buttons",
direction="left",
buttons=buttons,
pad={"r": 10, "t": 10},
showactive=True,
x=0.05,
xanchor="left",
y=1.3,
yanchor="top",
),
]
)
fig.show()