How to Order Bars in plotly Barchart in Python (Example)

 

Hello everyone! It is nice to be back with another interesting and educative tutorial. In this one, I will show you how you can order the bars of your plotly barchart in the Python programming language. We will create a pandas DataFrame and visualize it as a barplot, whose bars we shall order in both the ascending order and the descending order.

As always, here is a quick peek at how we are going to go about achieving our objective in this tutorial:

Believing you are ready, let’s dive into it!

 

Install plotly and pandas Libraries

To build a barchart in plotly, we will need to first install both the Python plotly library and the pandas library. In your Python IDE, please run the lines of code below to install plotly and pandas:

pip install plotly
pip install pandas

The lines of code above will install both libraries along with their dependencies.
 

Load plotly and pandas

Having installed plotly and pandas, we will need to import them into our environment so that we can make use of their functions to create a DataFrame and build a barchart. To do so, run the lines of code below:

import plotly.express as px
import pandas as pd

The above lines of code will install both libraries along with all the functions we need for our project.
 

Create pandas DataFrame

Let us now create a pandas DataFrame. We shall create a simple DataFrame comprising student names and their ages, which we shall visualize as a barchart eventually. You may run the code below:

df = pd.DataFrame({"Students":["James","Sheila","Max","Anna","Jenny","Harry"],
                   "Age":[24,20,25,23,18,27]})

To view the DataFrame, run print(df)

#   Students  Age
#0    James   24
#1   Sheila   20
#2      Max   25
#3     Anna   23
#4    Jenny   18
#5    Harry   27

 

Build Barplot

Now that we have created our DataFrame, let us now build a simple plotly barchart. In your IDE, please run the code below to build and visualize the barchart:

fig = px.bar(df,x = "Students",y = "Age",color = "Students")
fig.show()

We have just built a barchart in plotly, and have colored the bars by the student names so that each bar is distinct. However, you must have also noticed that the barchart is unordered, which does not make it look very nice. Let us now correct that.
 

Order Bars in Ascending Order

We are going to order the bars of our barchart in ascending order, such that the bars increase in height as the age values get higher. We are going to add one line of code to the previous code to achieve this. Please run the code below:

fig = px.bar(df,x = "Students",y = "Age",color = "Students")
fig.update_layout(xaxis = {"categoryorder":"total ascending"})
fig.show()

Great! Our barchart looks better now, and when you hover over the chart, you will see that the age values are in ascending order, which is exactly what we wanted to achieve.

Order Bars in Descending Order

We can also reverse the order of our barchart, such that the bars decrease in height as age values get lower i.e. a descending order barchart. To achieve this, in the fig.update_layout() function, we are going to change xaxis = {"categoryorder":"total ascending"} to xaxis = {"categoryorder":"total descending"} as shown below:

fig = px.bar(df,x = "Students",y = "Age",color = "Students")
fig.update_layout(xaxis = {"categoryorder":"total descending"})
fig.show()

And that is how to order the bars of a barchart in plotly using Python. All it took was just one line of code and a change of code arguments. It is very simple to do, and it makes your plot look nicer and easier to understand, especially at first glance.

I hope you liked this tutorial, and learned something of value. If yes, then watch out for more Python plotly tutorials on this blog, which I am very certain you would also enjoy. Thanks, and I will see you in the next one!

 

Video, Further Resources & Summary

Do you need more explanations on how to order the bars of your barchart in plotly using Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to order the bars of a barchart in plotly using Python in some more detail.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to order the bars of a bargraph in plotly using Python. In case you have further questions, you may leave a comment below.

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top