Search code examples
pythonplotlybar-chartstreamlitplotly-express

AttributeError: 'Figure' object has no attribute 'sort_values'


I am working on a data analysis page for my job and I have been trying to change the format of this bar graph to ascending order. I am using GitHub and Streamlit to deploy the information to a webpage.

This is the piece of code I used to create the actual bar graph:

figBARChartJune = px.bar(df_table, x="Issue Sub-Type", y="June 2023", text_auto=True)
figBARChartJune.update_traces(marker_color=['#12e195', '#01cdfe', '#05ffa1', '#b967ff', '#fffb96',
                                        '#001eff', '#e377c2', '#d05037', '#bcbd22', '#17becf',
                                        '#00FFFF', '#BFFF00'])
figBARChartJune.update_layout(
    xaxis_title="Issue Sub-Types",
    yaxis_title="Number of Issues",
    legend_title="Issue Sub-Types",
    font=dict(
        family="Times New Roman, monospace",
        size=12,
        color="RebeccaPurple"
    ),
    title={
        'text': "Total Number of Issues for June 2023",
    }
    )`

Bar graph

Nothing has been working so far to display the bar graph in ascending order. I put the number text on the stems themselves to show the exact amount.

Please help?

Thanks


Solution

  • That is because the bar graph in itself just plots the data as provided. It is not by default a Pareto (ordered).

    You can set this behavior by

    fig.update_layout(xaxis={'categoryorder':'total descending'})
    

    or alternatively

    fig.update_layout(xaxis={'categoryorder':'total ascending'})
    

    https://plotly.com/python/bar-charts/