How to Insert data at Plotly Chart Legend at streamlit app

Hi all. I’m trying to added some data at my chart legends but i don’t know how. I did search at plotly docs at Legends with Python but none of those examples available bring this feature. In the figure below is showed what i want to do. As you can see there is a legend of my chart and i want insert the data corresponded to the name of legend, i.g: UCL - 100, ICL - 50 and so on.

Here a real example of what i want:

Inkedxrchart_xbar_r_def.xml_Graph_cmd2o1_LI

One option would be fig.add_trace, this is a good example on how to accomplish that Add reference lines in scatter plot y axis - 📊 Plotly Python - Plotly Community Forum

If you continue to have any issues, feel free to share a repo or sample data and I will try to get a simple example.

Boa sorte

1 Like

Hi, thanks for sharing your idea but this example it’s isn’t what i want :pensive:

I can’t share the code either the link where my app are, but i can share the data used to generate these charts.

1

This is similar to your second graph. You just need to decide the value for your horizontal lines.

import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objects as go

d = {‘col1’: [100.02,100.06,100.01,99.99,100.02,100.01,100,99.99,100.03,100,99.99,100.01,100.02,100.04,99.98,100,100.02,100.03,100.02,100.01], ‘col2’:np.arange(1,21)}
df = pd.DataFrame(data=d)
#calculate your metrics and insert them as vars
ucl=100.065
lcl=99.98
mean_df=100.0125
ucl_array=np.ones(20)*ucl
lcl_array=np.ones(20)*lcl
mean_array=np.ones(20)*mean_df

fig=go.Figure()
fig = fig.add_trace(go.Scatter(x=df.col2,y=df.col1,mode=‘lines’,name=‘line1’))
fig = fig.add_trace(go.Scatter(x=df.col2,y=ucl_array,mode=‘lines’,name=‘ucl =’+str(ucl)))
fig = fig.add_trace(go.Scatter(x=df.col2,y=mean_array,mode=‘lines’,name=‘mean =’+str(mean_df)))
fig = fig.add_trace(go.Scatter(x=df.col2,y=lcl_array,mode=‘lines’,name=‘lcl =’+str(lcl)))

fig=fig.update_layout(showlegend=True)
st.plotly_chart(fig)

1 Like

Thanks for the help, i will try it tomorrow and come with a feedback!