By the end of this lesson, you will be able to:
Definition: Pygal is a module that can draw interactive charts in Python. It helps us transform numbers and data into beautiful, interactive visualizations! :bar_chart:
Think of data visualization like turning boring numbers into colorful stories! :art: Instead of looking at a list of numbers, we can create charts that make patterns and information jump out at us.
Pygal allows us to visualize our data in a human-readable way through various types of charts.
Here are the three main charts we'll master in this lesson:
Bar Chart - Perfect for comparing different amounts
Pie Chart - Great for showing parts of a whole
Radar Chart - Excellent for comparing multiple characteristics
:information_source: Remember! Each chart type has its own special purpose. Choose the one that best tells your data's story!
For a list of other supported charts, you can visit the official documentation.
Let's learn how to create our first chart! Follow these 5 simple steps:
Step One: Import the Pygal module
:bulb: Python Connection!
In text-based Python, this block creates:
import pygal
Step 2: Create your chart variable
:bulb: Troubleshooting
Make sure your chart name doesn't have spaces! Use underscores instead:
my_awesome_chart
notmy awesome chart
Step 3: Add a title to your chart
Step 4: Add your data
.add()
block[10, 20, 30]
:information_source: Remember! Even if you have just one value, it must be in a list:
[42]
not just42
Step 5: Display your chart
.render()
block to show your chart on the screen:bulb: Python Connection!
The complete Python code would look like:
python
import pygal my_chart = pygal.Bar() my_chart.title = "My First Chart" my_chart.add("Data Label", [10, 20, 30]) my_chart.render()
Now that we know the steps, let's create some amazing charts!
When adding a data entry, a label is represented in a rectangle bar.
The list for the data entry determines how many bars to present for that label.
Input | Output |
---|---|
![]() |
![]() |
The list for the data entry determines how many pie fractions to present for that label.
The total of all the data entries doesn't have to be 10 as shown below. You can have different values and it will display the proportions accordingly.
Input | Output |
---|---|
![]() |
![]() |
When adding a data entry, a label is plotted as a dot called radii (single data point).
For Radar Chart, you have to set the x_label for each individual radii.
The list for the data entry determines each plotted x_label dot with a line drawn through them that forms a polygon shape. The x_label must also be a list in a square bracket [], just like the .add() list.
Depending on the type of chart used, some can also have x_label to label the x-axis such as the bar chart and radar chart.
Input |
---|
![]() |
Output |
---|
![]() |