How To Create Animated Bar Chart Race
Step by Step Tutorial — Create a bar chart race animation
Building the hyped visualisation of 2019 using Python and Flourish
2019 was the year of bar charts race animation. Of course, this type of animation has been around for a while, its popularity peaked in the early months of 2019. Although I am not a big fan — as the same insights can be interpreted with a simple line graph — it is an interesting visualisation and unarguably aesthetically pleasing to watch.
As a result, this post is a step by st e p tutorial about how to create a Bar Chart Race Animation with Python and Flourish. In fact, Flourish will be the main tool for creating the visualisation, but some python scripting will also be required for formatting the dataset. I mean, you can skip the Python if data formatting can be done manually (in an acceptable time of course!).
The final output should look something like this: (In case you are wondering, Y-axis labels are written in Greek but that's just how the dataset was)
Alternatively, if the GIF preview above is lagging, you can view the visualisation online at:
Lastly, here is the GitHub repository with all the relevant code, and data:
Dataset
For the sake of this tutorial, I will be using a dataset taken from Cyprus' Open Data Portal. More precisely the dataset contains the number of people that applied for an unemployment allowance per economic activity category each month since 2013. It will be interesting to visualise how these numbers cycle over time and also a good chance to utilise some of the datasets offered in the national open data portal.
I have already included a link for the dataset in the data formatting code, but just for reference here is a link below:
The Plan
The plan is simple, as Flourish takes care of the heavy work for us. Conveniently, Flourish has a template for creating this particular visualisation. As such, we will use Python to transform the dataset into a format that fits the format of the Flourish template data. Then, the data will just be imported to Flourish, and the visualisation will be captured.
Step by Step
As promised this tutorial is step by step. So let's move on and follow the steps below.
Step 1: Create Flourish Account
Go ahead and create a Flourish account from the following link:
Step 2: Open up the Bar Chart Race Template
Open the Bar Chart Race Template using the link below and click "Create Visualisation":
Click on the "Data" tab and observe how the data should be formatted for the template to work. Note that each bar is a row and each time point is a column ( apart from the "region" and "Image URL" which are optional).
Step 3: Data Formatting
Let's go ahead and format the data to be imported to Flourish. I have already written the Python code for this in Colab so that there is no need for any environment setup:
Open up the Jupyter Notebook and save a copy to your drive as shown below:
When the notebook has been copied, you should be able to run the code, thus go ahead and run all the cells. The code will format the data and generate a data.csv file in the local Jupyter Notebook machine.
I won't be explaining how the code works in this post, but I have included comments in the notebook for easier interpretation.
Step 4: Download the formatted data and Import
The data.csv can be downloaded by expanding the left pane, clicking the "Files" tab, right-click the data.csv file (Click refresh if it is not already there) and hit "Download".
Step 5: Import file and fix column names
Go back to the Flourish template, click "Import your data" and select the data.csv that was just downloaded:
For some reason, Flourish transforms the month columns' names as dates of the format "dd/mm/YY", however, it does not look good on the visualisation so I let's rename the columns with the names that were given initially.
It is fairly easy to do that manually by opening the CSV file in a spreadsheet editor, copy the column names and paste them in the Flourish data tab.
Step 6: Tweak visualisation and preview
Back to the Preview tab, the visualisation should already be animating with your imported data. In addition, Flourish provides the chance to tweak the visualisation in terms of colours, layout, font sizes etc… so feel free to change those to match your preferences.
Lastly, click the "Full Screen" icon at the top left to open up a new web page that contains only the visualisation.
Step 7: Save the visualisation
By now you should be looking at the visualisation generated in a new window and admiring how neat it is! Well done! But it is time to save it somewhere.
There are two ways for that and depending on the situation, the one may be more preferable over the other.
- Save as HTML file: The full-screen preview of the visualisation can be saved as an independent HTML page locally. Right-click anywhere on the page and select "Save As.." and make sure that the Format is set to "Web page, Complete"
2. Record as GIF: Recoding the visualisation as GIF or any other video format makes it easy to share (in most cases). There are many programs available to do just that. If you are on Windows, definitely go with ScreenToGif and if on Mac with LiCEcap. The main reason for using these two is that they don't have a limitation on the length of the recording. Anyhow, feel free to use any software that achieves the same purpose.
Note that recording as GIF might introduce lag to the animation due to frame rate limitations.
The End
And that was the end of the tutorial folks. I hope it was straightforward enough to follow. The visualisation saved locally is ready to be shared in blog posts, presentations, websites or whatever else you like!
Flourish is a really amazing tool and although we used only free features, I would say that if you are into data visualisations, it worths paying to upgrade to a premium account.
Any feedback is more than welcome. 'Till next time!
How To Create Animated Bar Chart Race
Source: https://towardsdatascience.com/step-by-step-tutorial-create-a-bar-chart-race-animation-da7d5fcd7079
Posted by: scottmusur1939.blogspot.com

0 Response to "How To Create Animated Bar Chart Race"
Post a Comment