Effective information understanding and presentation may be achieved via the use of data visualization. Pie charts are a common choice for visually attractive data distribution representation. We’ll look at how to use Python to make animated pie charts in this blog article. For the purpose of creating dynamic and interactive visualizations, we will utilize the sjvisualizer package. This article will walk you through installing the necessary libraries and putting the necessary code into use to make animated pie charts in python.
Applications of Animated pie charts in python
Here are the top 4 applications of animated pie charts in python.
- Presenting data distribution in a visually engaging manner
- Showing the evolution of data over time
- Comparing proportions between different categories
- Adding interactivity to data visualizations
Implementations of Animated pie charts in python
Install the sjvisualizer library: Open your terminal or command prompt and execute the following command
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 from google drive
https://drive.google.com/drive/folders/1BafDv-Dy-13Z0frzYwrYvFEhZqifGq8z?usp=sharing
!pip install "sjvisualizer-0.0.7-py2.py3-none-any.whl"
Import the required modules:
from sjvisualizer import Canvas
from sjvisualizer import DataHandler
from sjvisualizer import PieRace
import time
import json
Define the main function:
def main(fps=60, duration=0.35):
number_of_frames = duration * 60 * fps
df = DataHandler.DataHandler(excel_file="Countries that smoke the most.xlsx",
number_of_frames=number_of_frames).df
canvas = Canvas.canvas()
bar_chart = PieRace.pie_plot(canvas=canvas.canvas, df=df)
canvas.add_sub_plot(bar_chart)
# add static text
canvas.add_title("Countries with the highest smoking rates", color=(0,132,255))
canvas.add_sub_title("Nation with High Smoking Rates", color=(0,132,255))
canvas.add_time(df=df, time_indicator="month")
canvas.play(fps=fps)
Here is an explanation of the above codes
- The code defines a function named
main
with two optional parameters:fps
(frames per second) andduration
. - The variable
number_of_frames
is calculated by multiplying the duration (in seconds) by 60 (seconds per minute) and the specifiedfps
. - The code initializes a
DataHandler
object by passing the name of an Excel file (“Countries that smoke the most.xlsx”) and thenumber_of_frames
as arguments. The resultingdf
object represents the data from the Excel file. - A
Canvas
object is created using thecanvas
function. - A pie chart race is created using the
pie_plot
function from thePieRace
module, passing thecanvas
object and thedf
object as arguments. The resultingbar_chart
object represents the pie chart race. - The
bar_chart
is added as a subplot to thecanvas
. - A title and sub-title s added to the
canvas
- A time indicator is added to the
canvas
using theadd_time
function, passing thedf
object and the time indicator as arguments. The time indicator can be set to “month” in this case. - The
canvas
is played with the specifiedfps
, resulting in the animation of the pie chart race.
Execute the main function:
if __name__ == "__main__":
main()
Conclusion
In this blog article, we learnt how to use the sjvisualizer module to generate animated pie charts in Python. The techniques listed make it simple to see how data is distributed and how it changes over time. Pie charts offer a simple method for comparing percentages and efficiently communicating information. Try out various datasets, then adapt the graphic components to your need. You can produce gorgeous animated pie charts for a variety of applications using Python’s power and data visualisation modules.
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