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.
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.
pygal.style
within 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
, open the terminal and run the following command:1pip install pygal
conda
, open the terminal and run the following command:1conda install -c conda-forge 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:
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')
The rendered file will be saved in the current directory.
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')
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')
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()
Note: you can also render SVG in the browser directly using the render_in_browser
command. It becomes constructive if you are using Jupyter notebook.
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')
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')
We will now learn how to plot basic pie charts.
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')
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')
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')
Maps are now kept separately to keep pygal a reasonably sized package.
Three map packages are available in pygal:
We will use the world map package to plot the maps.
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
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')
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()
For a list of country codes, see the Pygal documentation .
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')
For continent codes, see the pygal documentation .
There are three ways to style a chart:
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')
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
Neon
1 pygal.style import NeonStyle
2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=NeonStyle)
Light Solarized
1from pygal.style import LightSolarizedStyle
2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=LightSolarizedStyle)
Red Blue
1from pygal.style import RedBlueStyle
2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=RedBlueStyle)
Turquoise
1from pygal.style import TurquoiseStyle
2chart = pygal.StackedLine(fill=True, interpolate='cubic', style=TurquoiseStyle)
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')
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')
Saturate style
1from pygal.style import SaturateStyle
2saturate_style = SaturateStyle('#609f86')
3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=saturate_style)
Rotate style
1from pygal.style import RotateStyle
2dark_rotate_style = RotateStyle('#9e6ffe')
3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=dark_rotate_style)
Lighten Style
1from pygal.style import LightenStyle
2dark_lighten_style = LightenStyle('#004466')
3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=dark_lighten_style)
Desaturate style
1from pygal.style import DesaturateStyle
2desaturate_style = DesaturateStyle('#8322dd', step=8)
3chart = pygal.StackedLine(fill=True, interpolate='cubic', style=desaturate_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')
Some properties you need to remember which are supported by the style class:
Properties | Description |
---|---|
plot_background | The color of the chart area background |
background | The color of the image background |
foreground | The main foregrond color |
font_family | The main font family |
label_font_family | The label font family |
value_font_family | The print_values font family |
title_font_family | The title font family |
value_font_size | The print_values font size |
tooltip_font_size | The tooltip font size |
title_font_size | The title font size |
legend_font_size | The legend font size |
opacity | The opacity of chart element |
colors | The series color list |
value_colors | The print_values color list |
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.