🔥

Plotlyその2

2023/04/10に公開
import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=df1['x'], y=df1['y'],
    mode='markers', marker_color='blue',
    name=names[0], visible=visible[0]
))

fig.add_trace(go.Scatter(
    x=df2['x'], y=df2['y'],
    mode='markers',
    name=names[1], visible=visible[1]
))

fig.add_trace(go.Scatter(
    x=df3['x'], y=df3['y'],
    mode='markers',
    name=names[2], visible=visible[2]
))

# レイアウトの設定
fig.update_layout(
    title='Multiple Scatter Plot',
    xaxis_title='X Axis',
    yaxis_title='Y Axis',
    updatemenus=[
        dict(
            buttons=list([
                dict(
                    args=[{'visible': [True, False, False]}],
                    label=names[0],
                    method='update'
                ),
                dict(
                    args=[{'visible': [False, True, False]}],
                    label=names[1],
                    method='update'
                ),
                dict(
                    args=[{'visible': [False, False, True]}],
                    label=names[2],
                    method='update'
                ),
                dict(
                    args=[{'visible': [True] * len(names)}],
                    label='All',
                    method='update'
                )
            ]),
            direction='down',
            showactive=True,
            xanchor='left',
            yanchor='top'
        )
    ]
)

# グラフの表示
fig.show()

Discussion