A crucial component of data analysis and presentation is data visualization. To illustrate and compare categorical data across various groups or categories, stacked bar charts are frequently employed. In this blog article we’ll look at how to make animated stacked bar charts in Python. To create dynamic visualizations that are aesthetically pleasing, we will make use of the sjvisualizer package. You will have the skills required to produce interactive stacked bar charts in Python by the conclusion of this session.’
Applications of Animated Stacked bar charts in Python
Applications for animated stacked bar charts include:
- Animated stacked bar charts may be used to analyze and visualize market share data over time for various goods and services.
- Animated stacked bar charts are useful for showing survey results because they may demonstrate how responses are distributed among several categories.
- Animated stacked bar charts may be used to demonstrate trends and patterns in financial data, such as income and costs.
Implementation of Animated Stacked bar charts in Python
But before that you have to download this whl file from here. And place in your folder where your Jupyter notebook file is present.
https://github.com/SjoerdTilmans/sjvisualizer/blob/main/dist/sjvisualizer-0.0.7-py2.py3-none-any.whl
You can download the dataset and colors files from google drive
https://drive.google.com/drive/folders/1BafDv-Dy-13Z0frzYwrYvFEhZqifGq8z?usp=sharing
To create animated stacked bar charts in Python, follow these steps:
Step 1- Install Required Libraries
!pip install "sjvisualizer-0.0.7-py2.py3-none-any.whl"
Step 2- Import the Required Modules
from sjvisualizer import Canvas
from sjvisualizer import DataHandler
from sjvisualizer import StackedBarChart
Step 3- Define the main functions
def main(fps = 60, duration = 0.35):
number_of_frames = duration*60*fps
# load colors
with open('colors/colors.json') as f:
colors = json.load(f)
df = DataHandler.DataHandler(excel_file="data/DesktopOS.xlsx", number_of_frames=number_of_frames).df
canvas = Canvas.canvas()
# add bar chart
stacked = StackedBarChart.stacked_bar_chart(canvas=canvas.canvas, df=df, colors=colors, number_of_bars=25)
canvas.add_sub_plot(stacked)
# add static text
canvas.add_title("DESKTOP OS MARKET SHARE", color=(0,132,255))
canvas.add_sub_title("2009 - 2003", color=(0,132,255))
# add time indication
canvas.add_time(df=df, time_indicator="month")
# save colors for next run
with open("colors/colors.json", "w") as file:
json.dump(colors, file, indent=4)
canvas.play(fps=fps)
There are following steps involved while creating main function
- Create a Canvas class instance, which will act as the visual framework for our chart.
- Utilize the DataHandler class to load your data. Here, we’ll suppose that your stacked bar chart’s data are located in an Excel file called “DesktopOS.xlsx”.
- To create the stacked bar chart and add it to the canvas, use the StackedBarChart class.
- Improve your chart by including other components like titles and time indicators.
- Keep the chart’s colours in mind for next runs.
- Finally, use the canvas’s play() function to play the animation.
Step 4- Call the main functions
if __name__ == "__main__":
main()
Conclusion
In this blog article, we looked at how to use Python to make animated stacked bar charts. We produced dynamic and eye-catching visualisations by utilising the sjvisualizer library. When animated, stacked bar charts may be used to compare categorical data between groups and provide an interactive component to the visualisation. You can now make interesting stacked bar charts to clearly explain your data insights thanks to the knowledge you gained from this article.
If you like the article and would like to support me, make sure to:
- 👏 Like for this article and subscribe to our newsletter
- 📰 View more content on my DataSpoof website
- 🔔 Follow Me: LinkedIn| Youtube | Instagram | Twitter