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

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

add labels and legend in plotly express in streamlit

问题

来自plotly.subplots的make_subplots
import plotly.graph_objects as go
import plotly.express as px
导入streamlit as st

#索引是日期
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)
英文:

the following is my code:

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)     

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

# 使用 `update_layout` 可以实现这一点。这将在两个子图中添加标签和图例。

...
# 添加 traces 到子图中
fig.add_trace(fig1['data'][0], row=1, col=1, name='价格')
fig.add_trace(fig1['data'][1], row=1, col=1, name='计数')
fig.add_trace(fig2['data'][0], row=1, col=2, name='价格')
fig.add_trace(fig2['data'][1], row=1, col=2, name='计数')

fig.update_layout(
  title= "设置标题",
  xaxis_title="X轴标题",
  yaxis_title="Y轴标题",
  legend=dict(title="图例标题", traceorder="normal"),
  
  # 设置第一个子图的标题和坐标轴标签
  title_text="子图 1",
  xaxis1_title="X轴标题",
  yaxis1_text="Y轴标题",
  # 为第一个子图设置图例
  legend_title="设置图例标题",
  legend_traceorder="normal",
  legend_x=0.1,
  legend_y=1.0,
  
  # 设置第二个子图的坐标轴标签和标题
  xaxis2_title="X轴标题",
  yaxis2_title="Y轴标题",
  # 为第二个子图设置图例
  legend2_title="设置图例标题",
  legend2_traceorder="normal",
  legend2_x=0.1,
  legend2_y=1.0,
)
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.

...
# 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_text="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)

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:

确定