使用Plotly中的下拉菜单更改数据框。

huangapple go评论76阅读模式
英文:

Change dataframe using a dropdown menu in Plotly

问题

I want to show a single plotly figure where the showed dataframe must change according to a dropdown menu.
我的目标是显示一个Plotly图,其中显示的数据框根据下拉菜单的选择而变化。

I've tried the following code but the only I see changing is the title not the data showed.
我尝试了以下代码,但我只看到标题变化了,而数据没有变化。

My intention is to show either data1 or data2.
我的意图是显示data1或data2中的数据。

英文:

I want to show a single plotly figure where the showed dataframe must change according to a dropdown menu.
I've tried the following code but the only I see changing is the title not the data showed.
My intention is to show either data1 or data2.

import plotly.express as px
import plotly.io as pio

data1 = {'x': [1, 2, 3, 4, 5], 'y': [10, 15, 13, 17, 20]}
data2 = {'x': [1, 2, 3, 4, 5], 'y': [5, 8, 9, 12, 10]}

fig = px.line(data1, x='x', y='y', title="Data 1")
# fig.add_scatter(x=data2['x'], y=data2['y'], mode='lines')

fig.update_layout(
    updatemenus=[
        dict(
            active=0,
            buttons=list([
                dict(label="Data1",
                     method="update",
                     args=[
						 { "visible": [True]},
                           {
                               "title": "Data 1",
						   	   "data_frame":data1
                            },
                           ]),
                dict(label="Data2",
                     method="update",
                     args=[
						 { "visible": [True]},
						 {
                               "title": "Data 2",
						   	   "data_frame":data2
						 }
                           ]),
            ]),
        )
    ])

fig.show()

答案1

得分: 1

I think this works:

import plotly.express as px
import plotly.io as pio

data1 = {'x': [1, 2, 3, 4, 5], 'y': [10, 15, 13, 17, 20]}
data2 = {'x': [1, 2, 3, 4, 5], 'y': [5, 8, 9, 12, 10]}
data3 = {'x': [1, 2, 3, 4, 5], 'y': [1, 2, 1, 2, 2]}

fig = px.line(data1, x='x', y='y', title="Data 1")
fig.add_scatter(x=data2['x'], y=data2['y'], mode='lines')
fig.add_scatter(x=data3['x'], y=data3['y'], mode='lines')

fig.update_traces(visible=False)
fig.data[0].visible = True

steps = []
for i in range(len(fig.data)):
    step = dict(
        method="update",
        label="Data #" + str(i),
        args=[{"visible": [False] * len(fig.data)},
              {"title": "Slider switched to step: " + str(i)}],
    )
    step["args"][0]["visible"][i] = True
    steps.append(step)

sliders = [dict(
    active=0,
    buttons=steps
)]

fig.update_layout(updatemenus=sliders)

fig.show()

(Note: I've translated the code comments and strings into English, as they were originally in Chinese in your provided text.)

英文:

I think this works:

import plotly.express as px
import plotly.io as pio

data1 = {'x': [1, 2, 3, 4, 5], 'y': [10, 15, 13, 17, 20]}
data2 = {'x': [1, 2, 3, 4, 5], 'y': [5, 8, 9, 12, 10]}
data3 = {'x': [1, 2, 3, 4, 5], 'y': [1, 2, 1, 2, 2]}

fig = px.line(data1, x='x', y='y', title="Data 1")
fig.add_scatter(x=data2['x'], y=data2['y'], mode='lines')
fig.add_scatter(x=data3['x'], y=data3['y'], mode='lines')

fig.update_traces(visible=False)
fig.data[0].visible = True

steps = []
for i in range(len(fig.data)):
    step = dict(
        method="update",
        label="Data #" + str(i),
        args=[{"visible": [False] * len(fig.data)},
              {"title": "Slider switched to step: " + str(i)}],
    )
    step["args"][0]["visible"][i] = True
    steps.append(step)

sliders = [dict(
    active=0,
    buttons=steps
)]

fig.update_layout(updatemenus=sliders)

fig.show()

huangapple
  • 本文由 发表于 2023年5月14日 15:47:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定