Introduction
Pygal is a tool in Python, which is a programming language. It's used for making charts and graphs that look nice and are easy to understand. So before using Pygal, you have to install it. I am using Google Colab to execute my code. So I installed used the below code.
!pip install pygal
A Pie Chart
A Pie Chart is like a circular cake cut into slices. Each slice shows a part of the whole thing. For example, if you want to show how much of your day is spent on different activities like sleeping, working, and playing, a pie chart can show this in a clear way.
import pygal
# Sample data
data = {
'Python': 30,
'JavaScript': 25,
'Ruby': 20,
'Java': 15,
'C++': 10
}
# Create a pie chart
pie_chart = pygal.Pie()
pie_chart.title = 'Programming Language Popularity'
for language, percent in data.items():
pie_chart.add(language, percent)
# Save the chart to a file
pie_chart.render_to_file('pie_chart.svg')
Output
Code Explanation
-
Setting Up and Sample Data
- The code starts by importing the Pygal library. This is necessary to use the features of Pygal for creating charts.
- Then, it sets up some sample data. This data is like a small list where each item has a programming language name (like 'Python', 'JavaScript', etc.) and a number. The number represents something measured for each language, like how popular it is.
-
Creating the Pie Chart
- The code then begins to create a Pie Chart. A Pie Chart is a circular chart divided into slices to show numerical proportions.
- pie_chart = pygal.Pie(): This line creates a new Pie Chart.
- pie_chart.title = 'Programming Language Popularity': This sets the title of the chart.
-
Adding Data to the Chart
- The for loop in the code goes through each programming language and its corresponding percentage in the sample data.
- pie_chart.add(language, percent): For each language, this line adds a slice to the pie chart. The size of the slice depends on the percentage for that language.
-
Saving the Chart
- Finally, pie_chart.render_to_file('pie_chart.svg') saves the chart to a file named 'pie_chart.svg'. This file is in SVG format, which is a type of image file that keeps the quality of the image good, even if you make it bigger or smaller.
A Stacked Line Chart
A Stacked Line Chart is a bit different. Imagine you're keeping track of how much money you spend on food, clothes, and games over several months. A stacked line chart can show how each category adds up over time, all in one graph.
import pygal
# Sample data
data = {
'Python': [15, 30, 45, 60],
'JavaScript': [25, 55, 35, 20],
'Ruby': [5, 15, 8, 10]
}
# Create a stacked line chart
stackedline_chart = pygal.StackedLine(fill=True)
stackedline_chart.title = 'Programming Language Trends Over Time'
stackedline_chart.x_labels = ['2017', '2018', '2019', '2020']
for language, values in data.items():
stackedline_chart.add(language, values)
# Save the chart to a file
stackedline_chart.render_to_file('stackedline_chart.svg')
Output
Code explanation
-
Setting Up and Sample Data
- First, the code imports the Pygal library, which is needed to create the chart.
- Then, it sets up some sample data. This data is like a small table. Each row of the table has a programming language name ('Python', 'JavaScript', 'Ruby') and a list of numbers. These numbers represent something measured for each language over four years (2017, 2018, 2019, 2020). For example, it might be the popularity of the language or the number of projects using it.
-
Creating the Stacked Line Chart
- The code then starts making a Stacked Line Chart. This type of chart shows how different parts add up over time.
- stackedline_chart = pygal.StackedLine(fill=True): This line creates a new Stacked Line Chart. The fill=True part means that the areas under the lines in the chart will be filled with color.
- stackedline_chart.title = 'Programming Language Trends Over Time': This sets the title of the chart.
- stackedline_chart.x_labels = ['2017', '2018', '2019', '2020']: This sets the labels along the bottom of the chart (the x-axis). They represent the years.
-
Adding Data to the Chart
- The for loop in the code goes through each programming language and its corresponding values in the sample data.
- stackedline_chart.add(language, values): For each language, this line adds a line to the chart. The line represents the values for that language over the four years.
-
Saving the Chart
- Finally, stackedline_chart.render_to_file('stackedline_chart.svg') saves the chart to a file named 'stackedline_chart.svg'. This file is in SVG format, which is good for graphics because it keeps the image clear and sharp, no matter how much you zoom in or out.
Conclusion
This article shows you how to easily create two types of charts - Pie Charts and Stacked Line Charts - using Pygal, a Python library. The Pie Chart is great for showing how different parts make up a whole, and the Stacked Line Chart is perfect for tracking changes over time. By following the simple steps outlined, you can quickly turn your data into clear, professional-looking charts that can enhance any presentation or report.