close

How to sort bars in a bar plot in ascending order?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to sort bars in a bar plot in ascending order in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to sort bars in a bar plot in ascending order?

  1. How to sort bars in a bar plot in ascending order?

    Sort the dataframe with .sort_values("Speed") or .sort_values('Speed', ascending=False) after aggregating.

  2. sort bars in a bar plot in ascending order

    Sort the dataframe with .sort_values("Speed") or .sort_values('Speed', ascending=False) after aggregating.

Method 1

df.groupby(['Id']).median().sort_values("Speed").plot.bar()

Sort the dataframe with .sort_values("Speed") or .sort_values('Speed', ascending=False) after aggregating.

EDIT: so you need to do this:

result = a.groupby(["Id"])['Speed'].median().reset_index().sort_values('Speed')

and in sns.barplot add order:

sns.barplot(x='Id', y="Speed", data=a, palette=colors, order=result['Id'])
enter image description here

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read