pandas 在streamlit中添加plotly express中的标签和图例

68bkxrlz  于 2023-01-11  发布在  其他
关注(0)|答案(1)|浏览(163)

下面是我的代码:

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
import streamlit as st

#index is date
fig1 = px.line(plot_df2,y=['price','counting'],template="plotly_dark"})
fig2 = px.line(plot_df3,y=['price','counting'],template="plotly_dark")
fig = make_subplots(rows=1, cols=2)
fig.add_trace(fig1['data'][0], row=1, col=1)
fig.add_trace(fig1['data'][1], row=1, col=1)
fig.add_trace(fig2['data'][0], row=1, col=2)
fig.add_trace(fig2['data'][1], row=1, col=2 
st.plotly_chart(fig, theme=None, use_container_width=True)

我想添加标签,如价格和计数和图例,如'数据集绘图'到代码中,我该怎么做?
先谢了

4c8rllxm

4c8rllxm1#

你可以在update_layout的帮助下完成这个任务,这会给两个子图添加标签和图例。

...
# Add traces to the subplots
fig.add_trace(fig1['data'][0], row=1, col=1, name='Price')
fig.add_trace(fig1['data'][1], row=1, col=1, name='Counting')
fig.add_trace(fig2['data'][0], row=1, col=2, name='Price')
fig.add_trace(fig2['data'][1], row=1, col=2, name='Counting')

fig.update_layout(
  title= "Set Title",
  xaxis_title="X-axis Title",
  yaxis_title="Y-axis Title",
  legend=dict(title="Legend Title", traceorder="normal"),
  
  # Set first suplot title and axis labels
  title_tex="Subplot 1"
  xaxis1_title="x-axis Title"
  yaxis1_text="y-axis Title"
  # Set legend for first subplot
  legend_title="Set legend Title",
  legend_traceorder="normal",
  legend_x=0.1,
  legend_y=1.0,
  
  # Set axis labels and title for second subplot
  xaxis2_title="x-axis Title",
  yaxis2_title="y-axis Title",
  # set legend for second subplot
  legend2_title="Set legend Title",
  legend2_traceorder="normal",
  legend2_x=0.1,
  legend2_y=1.0,
)
st.plotly_chart(fig, theme=None, use_container_width=True)

相关问题