Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Line chart in Pygal
Pygal stands out among the different Python data visualisation tools. The SVG (Scalable Vector Graphics) outputs from Pygal strike an excellent balance between elegance and customization. This article is devoted to building line charts in Pygal and includes pertinent examples to aid with comprehension.
The development of line charts is the main emphasis of this guide, however Pygal has many other features and a wide range of chart styles to meet a variety of data visualisation requirements.
Pygal: A Quick Recap
An open-source Python package called Pygal is used to produce stunning SVG charts that are both interactive and browser-friendly. With Pygal charts, users may easily change the design, labels, tooltips, and other features.
Installation
Make sure Pygal is installed before making line charts. Install it using pip ?
pip install pygal
Getting Started with Line Charts in Pygal
A common tool in data visualisation, line charts can be used to compare several categories, show trends over time, and much more. The method used by Pygal to create line charts is simple and adaptable.
In Pygal, initialising a Line object and populating it with data is required to create a line chart. Data is added using the add() method, which accepts a list of y-values and a label for the data. By default, the x-values are index values.
Example 1: Basic Line Chart with One Data Series
In our first example, we'll make a straightforward line chart to depict a week's worth of temperature readings ?
import pygal
# Create a new line chart
line_chart = pygal.Line()
# Set the title
line_chart.title = 'Weekly Temperature'
# Add data
line_chart.add('Temperature', [20, 22, 23.5, 25, 27, 29.5, 30])
# Render the chart to console (SVG output)
print(line_chart.render())
line_chart.title is used in the code above to set the title of the chart, and the add() method is used to add data. The chart is rendered as SVG using the render() function.
Example 2: Line Chart with Multiple Data Series and Custom X-Labels
Consider the case when we want to compare the weekly temperature trends between two cities. This is how ?
import pygal
# Create a new line chart
line_chart = pygal.Line()
# Set the title
line_chart.title = 'Weekly Temperature Comparison'
# Set x-labels
line_chart.x_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# Add data
line_chart.add('London', [20, 22, 23.5, 25, 27, 29.5, 30])
line_chart.add('Paris', [23, 25, 27, 28, 30, 31, 33])
# Render the chart
print(line_chart.render())
line_chart.x_labels in this instance specifies the x-axis labels, which represent the days of the week. Two additional data series, one for each city, are being added.
Example 3: Line Chart with Null Values
Datasets from the real world frequently contain missing or null values. In line charts, Pygal smoothly handles these ?
import pygal
# Create a new line chart
line_chart = pygal.Line()
# Set the title
line_chart.title = 'Weekly Temperature (With Missing Data)'
# Set x-labels
line_chart.x_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# Add data with missing values
line_chart.add('Temperature', [20, None, 23.5, 25, None, 29.5, 30])
# Render the chart
print(line_chart.render())
In this illustration, we represent missing data with None. At null values, Pygal automatically breaks the line.
Example 4: Line Chart with Style Customization
Chart style customisation is also possible with Pygal. Here is an illustration of a line chart in a custom style ?
import pygal
from pygal.style import DarkStyle
# Create a new line chart with custom style
line_chart = pygal.Line(style=DarkStyle)
# Set the title
line_chart.title = 'Weekly Temperature (Dark Style)'
# Set x-labels
line_chart.x_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# Add data
line_chart.add('Temperature', [20, 22, 23.5, 25, 27, 29.5, 30])
# Render the chart
print(line_chart.render())
In this illustration, we apply DarkStyle to our line chart by importing it from pygal.style. Pygal has a variety of pre-built styles available, and you can even make your own.
Configuration Options
Pygal provides various configuration options to customize your line charts ?
import pygal
# Create line chart with configurations
line_chart = pygal.Line(
width=800,
height=400,
show_dots=True,
fill=True,
interpolate='cubic'
)
line_chart.title = 'Configured Line Chart'
line_chart.x_labels = ['Q1', 'Q2', 'Q3', 'Q4']
line_chart.add('Sales', [100, 120, 140, 160])
print(line_chart.render())
This example shows how to set chart dimensions, enable dots, add fill under the line, and use cubic interpolation for smoother curves.
Conclusion
Line charts are just the beginning of Pygal's dynamic, reliable, and aesthetically beautiful way to build SVG charts in Python. You can design a wide range of interactive charts to bring your data to life by utilising Pygal's capability. Use render() for inline display or render_to_file() to save charts as SVG files.
