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

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

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.

  1. import plotly.express as px
  2. import plotly.io as pio
  3. data1 = {'x': [1, 2, 3, 4, 5], 'y': [10, 15, 13, 17, 20]}
  4. data2 = {'x': [1, 2, 3, 4, 5], 'y': [5, 8, 9, 12, 10]}
  5. fig = px.line(data1, x='x', y='y', title="Data 1")
  6. # fig.add_scatter(x=data2['x'], y=data2['y'], mode='lines')
  7. fig.update_layout(
  8. updatemenus=[
  9. dict(
  10. active=0,
  11. buttons=list([
  12. dict(label="Data1",
  13. method="update",
  14. args=[
  15. { "visible": [True]},
  16. {
  17. "title": "Data 1",
  18. "data_frame":data1
  19. },
  20. ]),
  21. dict(label="Data2",
  22. method="update",
  23. args=[
  24. { "visible": [True]},
  25. {
  26. "title": "Data 2",
  27. "data_frame":data2
  28. }
  29. ]),
  30. ]),
  31. )
  32. ])
  33. fig.show()

答案1

得分: 1

I think this works:

  1. import plotly.express as px
  2. import plotly.io as pio
  3. data1 = {'x': [1, 2, 3, 4, 5], 'y': [10, 15, 13, 17, 20]}
  4. data2 = {'x': [1, 2, 3, 4, 5], 'y': [5, 8, 9, 12, 10]}
  5. data3 = {'x': [1, 2, 3, 4, 5], 'y': [1, 2, 1, 2, 2]}
  6. fig = px.line(data1, x='x', y='y', title="Data 1")
  7. fig.add_scatter(x=data2['x'], y=data2['y'], mode='lines')
  8. fig.add_scatter(x=data3['x'], y=data3['y'], mode='lines')
  9. fig.update_traces(visible=False)
  10. fig.data[0].visible = True
  11. steps = []
  12. for i in range(len(fig.data)):
  13. step = dict(
  14. method="update",
  15. label="Data #" + str(i),
  16. args=[{"visible": [False] * len(fig.data)},
  17. {"title": "Slider switched to step: " + str(i)}],
  18. )
  19. step["args"][0]["visible"][i] = True
  20. steps.append(step)
  21. sliders = [dict(
  22. active=0,
  23. buttons=steps
  24. )]
  25. fig.update_layout(updatemenus=sliders)
  26. 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:

  1. import plotly.express as px
  2. import plotly.io as pio
  3. data1 = {'x': [1, 2, 3, 4, 5], 'y': [10, 15, 13, 17, 20]}
  4. data2 = {'x': [1, 2, 3, 4, 5], 'y': [5, 8, 9, 12, 10]}
  5. data3 = {'x': [1, 2, 3, 4, 5], 'y': [1, 2, 1, 2, 2]}
  6. fig = px.line(data1, x='x', y='y', title="Data 1")
  7. fig.add_scatter(x=data2['x'], y=data2['y'], mode='lines')
  8. fig.add_scatter(x=data3['x'], y=data3['y'], mode='lines')
  9. fig.update_traces(visible=False)
  10. fig.data[0].visible = True
  11. steps = []
  12. for i in range(len(fig.data)):
  13. step = dict(
  14. method="update",
  15. label="Data #" + str(i),
  16. args=[{"visible": [False] * len(fig.data)},
  17. {"title": "Slider switched to step: " + str(i)}],
  18. )
  19. step["args"][0]["visible"][i] = True
  20. steps.append(step)
  21. sliders = [dict(
  22. active=0,
  23. buttons=steps
  24. )]
  25. fig.update_layout(updatemenus=sliders)
  26. 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:

确定