My goal with this project was to build an animation of a horizontal stacked bar chart using Python. The inspiration and the data used for this came from the charts on the ourworldindata.org website [1]. I set a challenge for myself to see if I could build the same type of chart that is able to show changes in energy consumption data over the years. More specifically, I looked at data for different countries’ energy consumption per capita and also the breakdown of the contributions of different sources such as natural gas, nuclear, hydroelectricity, etc. My code, along with comments detailing how the program works, can be found on GitHub.
The videos below show two different versions of the animation with a 16:9 aspect ratio (set to 1080p HD for best quality). The same videos with a 4:5 aspect ratio are available on my personal projects Instagram account.
A sample of countries were selected from the top 20 largest energy consumers in the world. In addition, the figures for the World average and the European Union average were included for comparison. We can see that the program automatically ranks all nations from the highest per capita consumption to the lowest. More countries can be added by simply adding a new key-value pair with data in the data source dictionary.
Video 1 – Animated Bar Chart version 1 which includes Canada and the US – two very large per capita consumers.
Video 2 – Version 2 of the animated bar chart, excluding Canada and the US to keep the countries’ bar lengths closer together
One key requirement I initially set was that the program should be able to accept any data formatted as a Python dictionary and then generate an ordered chart. More detail on how the dictionary is ordered can be seen in my code comments. The same scripts could be used to present other data, perhaps something like different countries’ government spending broken down by categories. This project could be expanded in the future by putting together more datasets and adding in a UI to make the program interactive.
References
[1] Energy Institute – Statistical Review of World Energy (2024); Population based on various sources (2023) – with major processing by Our World in Data. Available: https://ourworldindata.org/energy-mix