Line Chart Visualization using Pygal

Line Chart PyGal FeaImg

Python has different visualization packages that can be used to make charts, graphs, and plots of different kinds. Pygal is an open-source Python package that not only generates highly interactive plots but also generates SVG pictures of the graphs and plots, allowing us to use and modify them as needed. Pygal is very flexible and just requires a few lines of code to generate graphs.

Also Read: 4 Easy Plotting Libraries for Python With Examples

In this tutorial, we will explore the line chart visualizations that can be created using PyGal and how to download the same in SVG format.


Before we can begin examining pygal, it must be installed with pip install pygal. After installation, we will be importing pygal for visualization.

We will start our visualizations with a simple Line Chart using the code below. We will start off by creating an empty Line chart using the Line function and set the title of the chart using the title attribute.

Next, we will set the x_labels randomly, and then we will make line charts for four different programming languages and add the line label and values for each line plot. Lastly, we will save and render the plot in SVG format using the render_to_file method.

# Create an empty Line Chart
Line_Chart = pygal.Line()

# Set title of the Line Chart
Line_Chart.title = 'A demonstration of Line Chart using Pygal'

# Set x labels/values
Line_Chart.x_labels = map(str, range(2000, 2022)) 

# Adding Line Chart for each language popularity over the years
Line_Chart.add('Python', [None, None, 0, 16.1, 22, 30, 36.9, 45.1, 
                          46.1, 42.33, 37.11, 38.5,50.78, 53.62, 55.37, 
                          60.22, 60.5, 61.68, 68.65, 79.83, 80.25, 85.44]) 

Line_Chart.add('Kotlin', [None, None, None, None, None, None, 0, 3.5, 10.1, 
                          23.1, 35.2,24.03, 46.53, 46.74, 47.31, 59.93,
                          16.11, 22.94, 23.56,1.86, 16.07]) 

Line_Chart.add('C++', [0.99, 7.15, 8.39, 9.78, 14.0, 25.66, 33.3, 35.8, 40.11, 
                       54.92, 57.17, 60.14, 68.63, 73.11, 75.99, 79.37, 89.73, 
                       90.82, 95.71,60.66,61.43, 64.94]) 

Line_Chart.add('Java', [14.1, 15.8, 15.1, 8.8, 8, 10.1, 18.4, 15.1, 16.6, 
                        16.1, 17.4,19.27, 21.24, 29.29, 38.04, 43.24, 
                        54.0, 62.55,11.28, 12.08, 13.19,29.35]) 

# Save the plot in svg format
Line_Chart.render_to_file('Line_Chart_Demo.svg')
Line Chart Pygal
Line Chart Pygal

We can also visualize Horizontal Line Chart with the help of the HorizontalLine method as shown in the code below. The rest of the code stays exactly the same.

# Create an empty Line Chart
Line_Chart = pygal.HorizontalLine()

# Set title of the Line Chart
Line_Chart.title = 'A Horizontal Line Chart using Pygal'

# Set x labels/values
Line_Chart.x_labels = map(str, range(2000, 2022)) 

# Adding Line Chart for each language popularity over the years
Line_Chart.add('Python', [None, None, 0, 16.1, 22, 30, 36.9, 45.1, 
                          46.1, 42.33, 37.11, 38.5,50.78, 53.62, 55.37, 
                          60.22, 60.5, 61.68, 68.65, 79.83, 80.25, 85.44]) 

Line_Chart.add('Kotlin', [None, None, None, None, None, None, 0, 3.5, 10.1, 
                          23.1, 35.2,24.03, 46.53, 46.74, 47.31, 59.93,
                          16.11, 22.94, 23.56,1.86, 16.07]) 

Line_Chart.add('C++', [0.99, 7.15, 8.39, 9.78, 14.0, 25.66, 33.3, 35.8, 40.11, 
                       54.92, 57.17, 60.14, 68.63, 73.11, 75.99, 79.37, 89.73, 
                       90.82, 95.71,60.66,61.43, 64.94]) 

Line_Chart.add('Java', [14.1, 15.8, 15.1, 8.8, 8, 10.1, 18.4, 15.1, 16.6, 
                        16.1, 17.4,19.27, 21.24, 29.29, 38.04, 43.24, 
                        54.0, 62.55,11.28, 12.08, 13.19,29.35]) 

# Save the plot in svg format
Line_Chart.render_to_file('Horizontal_Line_Chart_Demo.svg')
Horizontal Line Chart Pygal
Horizontal Line Chart Pygal

We can also visualize Stacked Line Chart with the help of the StackedLine method as shown in the code below. We can pass a property fill as True to fill the line plot and be creative!

# Create an empty Line Chart
Line_Chart = pygal.StackedLine(fill=True)

# Set title of the Line Chart
Line_Chart.title = 'A Stacked Line Chart (Filled) using Pygal'

# Set x labels/values
Line_Chart.x_labels = map(str, range(2000, 2022)) 

# Adding Line Chart for each language popularity over the years
Line_Chart.add('Python', [None, None, 0, 16.1, 22, 30, 36.9, 45.1, 
                          46.1, 42.33, 37.11, 38.5,50.78, 53.62, 55.37, 
                          60.22, 60.5, 61.68, 68.65, 79.83, 80.25, 85.44]) 

Line_Chart.add('Kotlin', [None, None, None, None, None, None, 0, 3.5, 10.1, 
                          23.1, 35.2,24.03, 46.53, 46.74, 47.31, 59.93,
                          16.11, 22.94, 23.56,1.86, 16.07]) 

Line_Chart.add('C++', [0.99, 7.15, 8.39, 9.78, 14.0, 25.66, 33.3, 35.8, 40.11, 
                       54.92, 57.17, 60.14, 68.63, 73.11, 75.99, 79.37, 89.73, 
                       90.82, 95.71,60.66,61.43, 64.94]) 

Line_Chart.add('Java', [14.1, 15.8, 15.1, 8.8, 8, 10.1, 18.4, 15.1, 16.6, 
                        16.1, 17.4,19.27, 21.24, 29.29, 38.04, 43.24, 
                        54.0, 62.55,11.28, 12.08, 13.19,29.35]) 

# Save the plot in svg format
Line_Chart.render_to_file('Stacked_Filled_Line_Chart_Demo.svg')
Stacked Filled Line Chart Pygal
Stacked Filled Line Chart Pygal

I hope you learned something new and interesting through this tutorial.
Thank you for reading!

I would recommend you these tutorials as well:

  1. Introduction to Lollipop charts in Python
  2. How to Plot and Customize a Pie Chart in Python?
  3. Violin Plots in Python – A Simple Guide