Display legend items in customized order visualizing with plotly

61 Views Asked by At

Hi I am using the following code:

import streamlit as st
import plotly.express as px
import pandas as pd


def piechart(home_win_probability, draw_game, away_win_probability, title):

    home_win_probability = round(home_win_probability, 2)
    draw_game = round(draw_game, 2)
    away_win_probability = round(away_win_probability, 2)

    labels = ['Home Win', 'Draw Game', 'Away Win']
    values = [home_win_probability, draw_game, away_win_probability]

    data = pd.DataFrame({'Category': labels, 'Value': values})

    fig = px.pie(data, names='Category', values='Value', title=title)

    # Set hover template to display the rounded value
    fig.update_traces(hovertemplate=labels)

    # Display the pie chart in Streamlit using st.plotly_chart
    st.plotly_chart(fig)

    return()

When I type piechart(31.2,48.7,20.1,'Probabilities'):

What I see:

enter image description here

What I would like to see:

enter image description here

What I tried (unsucessfully): I tried to add the following line before st.plotly_chart(fig):

fig.update_layout(legend_traceorder=['Home Win', 'Draw Game', 'Away Win'])

Thanks,

0

There are 0 best solutions below