✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

How to change order of horizontal bar Chart y-axis values

I am not able to change the order of y-axis values in horizontal bar chart.

Code:

 fig = px.bar(
        dff_sub["city"].value_counts()[:10],
        orientation="h",
        title="Top 10 cities which are badly affected by Terrorism",
        labels={"index": "Cities", "value": "Attack Counts "},
        color=dff_sub["city"].value_counts()[:10],
        color_continuous_scale=["#bdbdbd", "#969696", "#737373", "#525252",],
        
    )

Chart looks like this:

I just want this thing in reverse order in graph. The higher value in top and lower in bottom.

@empet any help here pls?

PX plots values in the order they appear in the data frame, so one option would be to sort your inputs. Alternatively, you can use the category_orders keyword argument to force the bars into any given specific order.

Finally, you can use the approach documented here : https://plotly.com/python/bar-charts/#bar-chart-with-sorted-or-ordered-categories

1 Like