Creating a Bar Chart Race animation function using Python
Bar chart race function in python
A bar chart race changes the sequence of bars in an animated way. The data values keep changing every second in ascending and descending order as shown in the video in the final output section(bottom of the page).
On 21st February 2019, a tweet by Matt Navarra went viral ann gained approximately 10 million views(1 crore). The tweet showed an animated bar graph that showed the changing positions of bars of the top 15 brands for the past 19 years. Many a time we have also seen social media posts related to the bar graph race and it catches the attention of almost every data enthusiast out there.
All said about bar chart races, have you ever wondered how these animated charts are made, how are they implemented using Python, if you have then you are at the right place and this article is definitely for you.
A bar chart race for the number of COVID-19 cases in the states of India starting from 19th April 2020.
We can install a bar chart race with any of the following ways:
- pip install bar_chart_race
- conda install -c conda-forge bar_chart_race
Starting with the code
1) Importing necessary libraries.
2) Reading our dataset.
3) Convert our original dataset into that form on which we can apply our bar chart race function. (Here we used pivot table function). Using the data. pivot function to create data in a pivot table so that it looks more in place to apply the bar chart race.
4) Further data manipulation using sort_values function and dropping a column.
The final data frame is prepared for the final function to be applied to the bar chart race function.
Main function [ bar_chart_race]
There is one main function, bar_chart_race, which will be used to create a bar chart race.
Final Output (Bar Chart Race):
Bar chart races using Python is a very good visualization technique to present it in front of the clients during month-end, quarter-end, or during the annual general meetings of the companies.
This was our output. We can customize our bar chart race according to our preferences. Full code can also be found in the link below:
Github Link: https://github.com/Sid-Yo/Covid19_BarChartRace
Siddhant Srivastava is a Data Science Enthusiast who has completed IBM Data Science Professional certification and enjoys using his skills to contribute to the exciting technological advances that happen in the Data Sciences field. He is currently pursuing a Bachelor’s Degree in Electronics and Communication, he is obsessed with all tech-related things and spends his free time exploring new data sets.