Author avatar

Gaurav Singhal

Charts in Pygal

Gaurav Singhal

  • Feb 5, 2020
  • 16 Min read
  • 7,009 Views
  • Feb 5, 2020
  • 16 Min read
  • 7,009 Views
Data
Pygal

Introduction

Pygal is a Python API that enables us to build SVG (scalar vector graphic) graphs and charts in a variety of styles. In this guide, we will learn how to use pygal to apply different methods to visualize data interactively and dynamically. We will also see how to plot in maps using the pygal_maps_world package of pygal and properties of custom styling.

About Pygal

Pygal is highly customizable yet extremely simple, a rare combination. We can create line graphs, bar graphs, histograms, pie charts, maps, and a whole lot more. From there, we can further customize the look and feel of the graphs.

Advantages of Pygal

  • It specializes in creating SVGs.
  • We can easily integrate pygal with flask and Django/Flask apps.
  • To keep it a reasonable size package, three map packages are kept separately.
  • We can customize charts using pygal.style within pygal.

Installation of Pygal

There are no dependencies for installing pygal. It's available for Python 2.7+, assuming that you have Python and pip installed on your system.

  • pip install: To install using pip, open the terminal and run the following command:
1pip install pygal
terminal
  • conda Install: To install using conda, open the terminal and run the following command:
1conda install -c conda-forge pygal
terminal

Charts in Pygal

I assume you have basic knowledge of python, you have installed Python 3 on your system, and you have a web browser. We'll look at how to create each of the following charts:

  • Line graphs
  • Bar charts
  • Pie charts
  • Maps
  • Customized charts

Line Graphs

Basic Example

We will take some random data and look at how to plot that data .

1#importing pygal
2import pygal
3# creating line chart object
4line_chart = pygal.Line()
5# naming the title
6line_chart.title = 'A vs B vs C vs D'
7# set  the range of plot
8line_chart.x_labels = map(str, range(0, 20))
9# adding lines
10line_chart.add('A', [None, None,0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
11line_chart.add('B',  [None, None, None,10, 0, 2, 5,None, 0,  3.9, 10.8, 3.8, 5.3])
12line_chart.add('C',      [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 5.7, 4.8, 6.2, 6.6, 0.1])
13line_chart.add('D',  [9.2, 19.4, 15.3,  8.9,None,    9, 10.4,  10,  5.8,  6.7,  6.8,  7.5])
14# rendering the file
15line_chart.render_to_file('basic_line_chart.svg')
python

The rendered file will be saved in the current directory.

Horizontal Line

We will plot the same data from the example above horizontally.

1# importing pygal
2import pygal
3# creating horizontal line chart object
4line_chart = pygal.HorizontalLine()
5# add title
6line_chart.title = 'A vs B vs C vs D'
7line_chart.x_labels = map(str, range(0, 20))
8line_chart.add('A', [None, None,0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
9line_chart.add('B',  [None, None, None,10, 0, 2, 5,None, 0,  3.9, 10.8, 3.8, 5.3])
10line_chart.add('C',      [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 5.7, 4.8, 6.2, 6.6, 0.1])
11line_chart.add('D',  [9.2, 19.4, 15.3,  8.9,None,    9, 10.4,  10,  5.8,  6.7,  6.8,  7.5])
12# rendering file
13line_chart.render_to_file('horizontal_line_chart.svg')
python

Stacked Line Chart.

1# importing pygal
2import pygal
3# creating horizontal line chart object
4line_chart = pygal.StackedLine(fill=True)
5# add title
6line_chart.title = 'A vs B vs C vs D'
7line_chart.x_labels = map(str, range(0, 20))
8line_chart.add('A', [None, None,0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
9line_chart.add('B',  [None, None, None,10, 0, 2, 5,None, 0,  3.9, 10.8, 3.8, 5.3])
10line_chart.add('C',      [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 5.7, 4.8, 6.2, 6.6, 0.1])
11line_chart.add('D',  [9.2, 19.4, 15.3,  8.9,None,    9, 10.4,  10,  5.8,  6.7,  6.8,  7.5])
12# rendering file
13line_chart.render_to_file('stacked_line_chart.svg')
python

Bar Graphs

Basic Example

1# First import pygal
2import pygal
3# Then create a bar graph object
4bar_chart = pygal.Bar()
5# Add some values
6bar_chart.add('Sequence Series', [0, 2, 4, 6, 8, 5, 6, 7, 21, 34, 55])
7# save the svgs
8bar_chart.render_in_browser() 
python

Note: you can also render SVG in the browser directly using the render_in_browsercommand. It becomes constructive if you are using Jupyter notebook.

Stacked Bar Graph

In this example, we will look at how to create a stacked bar using multiple series.

1# First import pygal
2import pygal
3# Then create a bar graph object
4bar_chart = pygal.Bar()
5# Then create a bar graph
6bar_chart = pygal.StackedBar()
7# adding random values
8bar_chart.add('Series A', [0, 50, 1, 2, 3, 5, 8, 3, 1, 44, 5])
9bar_chart.add('Series B', [1, 4, 1, 5, 2, 7, 4, 5, 7, 9, 8])
10# this will render the svgs in browser
11bar_chart.render_to_file('stacked_bar_chart.svg') 
python

Horizontal Multiple Series Bar Chart

1# First import pygal
2import pygal
3# Then create a bar graph object
4bar_chart = pygal.Bar()
5# Then create a bar graph
6line_chart = pygal.HorizontalBar()
7line_chart.title = 'Random data'
8line_chart.add('A', 20.5)
9line_chart.add('B', 36.7)
10line_chart.add('C', 6.3)
11line_chart.add('D', 4.5)
12line_chart.add('E', 80.3)
13# rendering the file
14line_chart.render_to_file('Horizontal_bar_chart.svg')
python

Pie Charts

We will now learn how to plot basic pie charts.

Basic Example

1# importing pygal
2import pygal
3# creating pie_chart object
4pie_chart = pygal.Pie()
5pie_chart.title = 'random data'
6# adding random data
7pie_chart.add('A', 20.5)
8pie_chart.add('B', 36.0)
9pie_chart.add('C', 35.9)
10pie_chart.add('D', 5.5)
11pie_chart.add('E', 1.3)
12# rendering the svg to the file
13pie_chart.render_to_file('pie_chart.svg')
python

Multiple Series Pie

1#importing pygal
2import pygal
3# creating pie_chart object
4pie_chart = pygal.Pie()
5pie_chart.title = 'random data'
6# adding random data
7pie_chart.add("Series A", [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
8pie_chart.add("Series C", [0, 1, 1, 2, 3, 7, 5, 10, 20, 32, 35])
9pie_chart.add("Series B", [0, 1, 1, 4, 5, 5.5, 7, 12, 26, 24, 45])
10# rendering the svgs
11pie_chart.render_to_file('multiple_pie_chart.svg')
python

Donut

1# importing pygal
2import pygal
3# creating pie_chart object
4pie_chart = pygal.Pie(inner_radius=.4)
5pie_chart.title = 'random data'
6# adding random data
7pie_chart.add('A', 20.5)
8pie_chart.add('B', 36.0)
9pie_chart.add('C', 35.9)
10pie_chart.add('D', 5.5)
11pie_chart.add('E', 80.3)
12# rendering the svg to file
13pie_chart.render_to_file('donut_chart.svg')
python

Maps

Maps are now kept separately to keep pygal a reasonably sized package.

Three map packages are available in pygal:

  • World map
  • French map
  • Swiss map

We will use the world map package to plot the maps.

Installation

Before plotting, we have to install the world map package on our system.

  • pip install - To install using pip, open the terminal and run the following command:

    1pip install pygal_maps_world
    terminal

Basic Example

1import pygal
2# importing World_map package 
3from pygal.maps.world import World
4# creating object
5worldmap_chart = pygal.maps.world.World()
6# adding title
7worldmap_chart.title = 'Some countries'
8# add data
9worldmap_chart.add('F countries', ['fr', 'fi'])
10worldmap_chart.add('I countries', ['in', 'il','iq'])
11worldmap_chart.add('M countries', ['ma', 'mc', 'md', 'me', 'mg',
12                                   'mk', 'ml', 'mm', 'mn', 'mo',
13                                   'mr', 'mt', 'mu', 'mv', 'mw',
14                                   'mx', 'my', 'mz'])
15worldmap_chart.add('U countries', ['ua', 'ug', 'us', 'uy', 'uz'])
16# rendering the svg to file
17worldmap_chart.render_to_file('basic_map.svg')
python

Adding Values to Map

1import pygal
2# importing World_map package 
3from pygal.maps.world import World
4# creating the object
5worldmap_chart = pygal.maps.world.World()
6# adding the title.
7worldmap_chart.title = 'random data'
8# adding the random values.
9worldmap_chart.add('values', {
10  'af': 14,
11  'bd': 1,
12  'by': 3,
13  'cn': 1000,
14  'gm': 9,
15  'in': 1,
16  'ir': 314,
17  'iq': 129,
18  'jp': 7,
19  'kp': 6,
20  'pk': 1,
21  'ps': 6,
22  'sa': 79,
23  'so': 6,
24  'sd': 5,
25  'tw': 6,
26  'ae': 1,
27  'us': 43,
28  'ye': 28
29})
30# directly rendering the file to browser 
31worldmap_chart.render_in_browser()
python

For a list of country codes, see the Pygal documentation .

Continents Chart

In pygal, we also have access to continents as well.

1import pygal
2# importing World_map package 
3from pygal.maps.world import World
4# creating object
5worldmap_chart = pygal.maps.world.World()
6supra = pygal.maps.world.SupranationalWorld()
7# adding the values
8supra.add('Asia', [('asia', 1)])
9supra.add('Europe', [('europe', 2)])
10supra.add('Africa', [('africa', 3)])
11supra.add('North america', [('north_america', 4)])
12supra.add('South america', [('south_america', 5)])
13supra.add('Oceania', [('oceania', 6)])
14supra.add('Antartica', [('antartica', 7)])
15# rendering the svgs
16supra.render_to_file('continents.svg')
python

For continent codes, see the pygal documentation .

Style Charts

There are three ways to style a chart:

  • Built-in styles
  • Parametric style
  • Custom style

Built-in Styles

1# importing built-in style
2from pygal.style import LightGreenStyle
3# creating the object
4chart = pygal.StackedLine(fill=True, interpolate='cubic', style=LightGreenStyle)
5# adding the random
6chart.add('A', [10, 30,  5, 16, 13, 3,  7])
7chart.add('B', [25, 2,  3,  4,  5, 7, 12])
8chart.add('C', [6, 10, 9,  7,  3, 15,  0])
9chart.add('D', [2,  3, 5,  4, 12, 18,  5])
10chart.add('E', [7,  4, 4,  1,  2, 25, 0])
11chart.render_to_file('style_chart 1.svg')
python

Pygal provides 14 types of built-in styles, including the following:

  • Default

    1from pygal.style import DefaultStyle
    2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=DefaultStyle
    python
  • Neon

    1 pygal.style import NeonStyle
    2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=NeonStyle)
    python
  • Light Solarized

    1from pygal.style import LightSolarizedStyle
    2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=LightSolarizedStyle)
    python
  • Red Blue

    1from pygal.style import RedBlueStyle
    2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=RedBlueStyle)
    python
  • Turquoise

    1from pygal.style import TurquoiseStyle
    2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=TurquoiseStyle)
    python

Parametric Style

A parametric style is initiated with a default color, and other shades are generated from that color.

1# import lightenstyle theme
2from pygal.style import LightenStyle
3dark_lighten_style = LightenStyle('#336676')
4# creating the object
5chart = pygal.StackedLine(fill=True, interpolate='cubic', style=dark_lighten_style)
6chart.add('A', [10, 3,  5, 16, 13, 3,  8])
7chart.add('B', [50, 52,  3,  2,  5, 7, 27])
8chart.add('C', [6, 10, 79,  7,  3, 1,  7])
9chart.add('D', [2,  3, 45,  9, 12, 9,  5])
10chart.add('E', [7,  4, 22,  41,  82, .50, 5])
11chart.render_to_file('parameterized_chart.svg')
python

There are five types of parametric styles in pygal.

  • Dark style

    1# importing the dark style
    2from pygal.style import DarkStyle
    3# creating the object
    4chart = pygal.StackedLine(fill=True, interpolate='cubic', style=DarkStyle)
    5# adding random values
    6chart.add('A', [10, 30,  5, 16, 13, 3,  7])
    7chart.add('B', [25, 2,  3,  4,  5, 7, 12])
    8chart.add('C', [6, 10, 9,  7,  3, 15,  0])
    9chart.add('D', [2,  3, 5,  4, 12, 18,  5])
    10chart.add('E', [7,  4, 4,  1,  2, 25, 0])
    11chart.render_to_file('style_chart.svg')
    python

  • Saturate style

    1from pygal.style import SaturateStyle
    2saturate_style = SaturateStyle('#609f86')
    3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=saturate_style)
    python
  • Rotate style

    1from pygal.style import RotateStyle
    2dark_rotate_style = RotateStyle('#9e6ffe')
    3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=dark_rotate_style)
    python
  • Lighten Style

    1from pygal.style import LightenStyle
    2dark_lighten_style = LightenStyle('#004466')
    3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=dark_lighten_style)
    python
  • Desaturate style

    1from pygal.style import DesaturateStyle
    2desaturate_style = DesaturateStyle('#8322dd', step=8)
    3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=desaturate_style)
    python

Custom Style

We will now look at how to customize a chart using the style class .

1import pygal
2# importing the style object from pygal.style
3from pygal.style import Style
4# customizing the plot using the properties of custom_style object.
5custom_style = Style(
6  background='transparent',
7  plot_background='transparent',
8  foreground='#53E89B',
9  foreground_strong='#53A0E8',
10  foreground_subtle='#630C0D',
11  opacity='.6',
12  opacity_hover='.9',
13  transition='400ms ease-in',
14  colors=('#E853A0', '#E8537A', '#E95355', '#E87653', '#E89B53'))
15# creating the object
16chart = pygal.StackedLine(fill=True, interpolate='cubic', style=custom_style)
17# adding the values
18chart.add('A', [1, 3,  5, 16, 13, 3,  7])
19chart.add('B', [5, 2,  23,  2,  5, 7, 17])
20chart.add('C', [6, 10, 9,  57,  3, 1,  0])
21chart.add('D', [2,  3, 45,  9, 12, 0,  5])
22chart.add('E', [7,  4, 2,  1,  2, 10, 0])
23# render the file
24chart.render_to_file('custom_style_chart.svg')
python

Some properties you need to remember which are supported by the style class:

PropertiesDescription
plot_backgroundThe color of the chart area background
backgroundThe color of the image background
foregroundThe main foregrond color
font_familyThe main font family
label_font_familyThe label font family
value_font_familyThe print_values font family
title_font_familyThe title font family
value_font_sizeThe print_values font size
tooltip_font_sizeThe tooltip font size
title_font_sizeThe title font size
legend_font_sizeThe legend font size
opacityThe opacity of chart element
colorsThe series color list
value_colorsThe print_values color list

Conclusion

In this tutorial, we used the pygal library to plot resourceful, dynamic, and interactive visualizations using different types of methods and custom styles. We also plotted on a world map using the pygal_maps_world package available in Python, and we saw how easy it is to make SVGTs in pygal. After reading this guide, you should have all the knowledge needed to create visually appealing graphs and charts with Python pygal.

Feel free to ask me any questions at CodeAlphabet.