在Streamlit中使用Plotly Express添加标签和图例。

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

add labels and legend in plotly express in streamlit

问题

  1. 来自plotly.subplotsmake_subplots
  2. import plotly.graph_objects as go
  3. import plotly.express as px
  4. 导入streamlit as st
  5. #索引是日期
  6. fig1 = px.line(plot_df2,y=['price','counting'],template="plotly_dark")
  7. fig2 = px.line(plot_df3,y=['price','counting'],template="plotly_dark")
  8. fig = make_subplots(rows=1, cols=2)
  9. fig.add_trace(fig1['data'][0], row=1, col=1)
  10. fig.add_trace(fig1['data'][1], row=1, col=1)
  11. fig.add_trace(fig2['data'][0], row=1, col=2)
  12. fig.add_trace(fig2['data'][1], row=1, col=2
  13. # 在这里添加标签和图例
  14. st.plotly_chart(fig, theme=None, use_container_width=True)
英文:

the following is my code:

  1. from plotly.subplots import make_subplots
  2. import plotly.graph_objects as go
  3. import plotly.express as px
  4. import streamlit as st
  5. #index is date
  6. fig1 = px.line(plot_df2,y=['price','counting'],template="plotly_dark"})
  7. fig2 = px.line(plot_df3,y=['price','counting'],template="plotly_dark")
  8. fig = make_subplots(rows=1, cols=2)
  9. fig.add_trace(fig1['data'][0], row=1, col=1)
  10. fig.add_trace(fig1['data'][1], row=1, col=1)
  11. fig.add_trace(fig2['data'][0], row=1, col=2)
  12. fig.add_trace(fig2['data'][1], row=1, col=2
  13. st.plotly_chart(fig, theme=None, use_container_width=True)

i want to add labels like price and count and legend like 'the dataset plot' to the code how can i do it?

thanks in advance

答案1

得分: 1

  1. # 使用 `update_layout` 可以实现这一点。这将在两个子图中添加标签和图例。
  2. ...
  3. # 添加 traces 到子图中
  4. fig.add_trace(fig1['data'][0], row=1, col=1, name='价格')
  5. fig.add_trace(fig1['data'][1], row=1, col=1, name='计数')
  6. fig.add_trace(fig2['data'][0], row=1, col=2, name='价格')
  7. fig.add_trace(fig2['data'][1], row=1, col=2, name='计数')
  8. fig.update_layout(
  9. title= "设置标题",
  10. xaxis_title="X轴标题",
  11. yaxis_title="Y轴标题",
  12. legend=dict(title="图例标题", traceorder="normal"),
  13. # 设置第一个子图的标题和坐标轴标签
  14. title_text="子图 1",
  15. xaxis1_title="X轴标题",
  16. yaxis1_text="Y轴标题",
  17. # 为第一个子图设置图例
  18. legend_title="设置图例标题",
  19. legend_traceorder="normal",
  20. legend_x=0.1,
  21. legend_y=1.0,
  22. # 设置第二个子图的坐标轴标签和标题
  23. xaxis2_title="X轴标题",
  24. yaxis2_title="Y轴标题",
  25. # 为第二个子图设置图例
  26. legend2_title="设置图例标题",
  27. legend2_traceorder="normal",
  28. legend2_x=0.1,
  29. legend2_y=1.0,
  30. )
  31. st.plotly_chart(fig, theme=None, use_container_width=True)
英文:

You can accomplish that with the help of update_layout. This should add labels and legend to both subplots.

  1. ...
  2. # Add traces to the subplots
  3. fig.add_trace(fig1['data'][0], row=1, col=1, name='Price')
  4. fig.add_trace(fig1['data'][1], row=1, col=1, name='Counting')
  5. fig.add_trace(fig2['data'][0], row=1, col=2, name='Price')
  6. fig.add_trace(fig2['data'][1], row=1, col=2, name='Counting')
  7. fig.update_layout(
  8. title= "Set Title",
  9. xaxis_title="X-axis Title",
  10. yaxis_title="Y-axis Title",
  11. legend=dict(title="Legend Title", traceorder="normal"),
  12. # Set first suplot title and axis labels
  13. title_text="Subplot 1"
  14. xaxis1_title="x-axis Title"
  15. yaxis1_text="y-axis Title"
  16. # Set legend for first subplot
  17. legend_title="Set legend Title",
  18. legend_traceorder="normal",
  19. legend_x=0.1,
  20. legend_y=1.0,
  21. # Set axis labels and title for second subplot
  22. xaxis2_title="x-axis Title",
  23. yaxis2_title="y-axis Title",
  24. # set legend for second subplot
  25. legend2_title="Set legend Title",
  26. legend2_traceorder="normal",
  27. legend2_x=0.1,
  28. legend2_y=1.0,
  29. )
  30. st.plotly_chart(fig, theme=None, use_container_width=True)

huangapple
  • 本文由 发表于 2023年1月9日 05:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051306.html
匿名

发表评论

匿名网友

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

确定