Prerequisites: having Python installed on your pc or being registered for Google Colaboratory.
First create a new directory for the project (if you use Google Colaboratory skip to step 1)
Then create a new virtual environment, and activate it
python3 -m venv venv source venv/bin/activate
If you haven’t already, install pandas
pip3 install pandas
Install the library we’ll use to create the bar chart race named, you won’t guess it, bar_chart_race
pip3 instll bar_chart_race
Now that we have the necessary libraries installed, let’s load our data.
In this tutorial, we’ll create a bar chart animation of the 2020 MotoGP World Championship’s title race. If you’re not familiar with this sport — that’s bad — don’t worry.
I will sum it up briefly for you to better understand what the data means.
MotoGP is the premier class of motorcycle road racing. A championship is held every year and consists of many races (or Grand Prix). After each Grand Prix, each rider gains some points depending on his final position at the GP. At the end of the season, the rider with the most point wins the championship.
You can find the dataset here.
Now the cool part: coding!
import pandas as pd import bar_chart_race as bcr
df = pd.read_csv('./2020-championship.csv') df = df.set_index('race')
The final result is only one line of code away
bcr.bar_chart_race(df=df, title='2020 MotoGP Championship Race', orientation='h', sort='desc', n_bars=10, steps_per_period=40, period_length=2000)
Ok, we’ve done with the coding. You can save and run the program, and you will obtain as output the bar chart race video.
Congrats, you’ve finished. As easy as that.
That was just a short introduction to this library. You can unleash your imagination creating much more advanced races with it.