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 book, 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.

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.

Installation

Make sure Pygal is installed before making line charts. Otherwise, use pip:

pip install pygal

Creating a Basic Line Chart

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.

Practical Examples of Line Charts in Pygal

Now let's use Pygal to make some useful line charts.

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
line_chart.render_to_file('line_chart.svg')

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 saved to an SVG file using the render_to_file() 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 = map(str, range(1, 8))

# 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
line_chart.render_to_file('line_chart_comparison.svg')

line_chart in this instance.The x-axis labels, which represent the days of the week, are specified using the x_labels function. 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 = map(str, range(1, 8))

# Add data
line_chart.add('Temperature', [20, None, 23.5, 25, None, 29.5, 30])

# Render the chart
line_chart.render_to_file('line_chart_missing_data.svg')

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 unique 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 = map(str, range(1, 8))

# Add data
line_chart.add('Temperature', [20, 22, 23.5, 25, 27, 29.5, 30])

# Render the chart
line_chart.render_to_file('line_chart_dark_style.svg')

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.

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.

Although Pygal makes it simple to create line charts, mastering the art of data visualisation requires understanding your data, discerning the type of visual representation that is most appropriate for your data, and learning how to use it.

Updated on: 18-Jul-2023

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements