Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Charts in Pygal

See how Pygal - a Python API used to build Scalar Vector Graphic (SVG) graphs and charts - can visualize data interactively and dynamically.

Feb 5, 2020 • 16 Minute Read

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:
      pip install pygal
    
  • conda Install: To install using conda, open the terminal and run the following command:
      conda install -c conda-forge pygal
    

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 .

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

The rendered file will be saved in the current directory.

Horizontal Line

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

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

Stacked Line Chart.

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

Bar Graphs

Basic Example

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

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.

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

Horizontal Multiple Series Bar Chart

      # First import pygal
import pygal
# Then create a bar graph object
bar_chart = pygal.Bar()
# Then create a bar graph
line_chart = pygal.HorizontalBar()
line_chart.title = 'Random data'
line_chart.add('A', 20.5)
line_chart.add('B', 36.7)
line_chart.add('C', 6.3)
line_chart.add('D', 4.5)
line_chart.add('E', 80.3)
# rendering the file
line_chart.render_to_file('Horizontal_bar_chart.svg')
    

Pie Charts

We will now learn how to plot basic pie charts.

Basic Example

      # importing pygal
import pygal
# creating pie_chart object
pie_chart = pygal.Pie()
pie_chart.title = 'random data'
# adding random data
pie_chart.add('A', 20.5)
pie_chart.add('B', 36.0)
pie_chart.add('C', 35.9)
pie_chart.add('D', 5.5)
pie_chart.add('E', 1.3)
# rendering the svg to the file
pie_chart.render_to_file('pie_chart.svg')
    

Multiple Series Pie

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

Donut

      # importing pygal
import pygal
# creating pie_chart object
pie_chart = pygal.Pie(inner_radius=.4)
pie_chart.title = 'random data'
# adding random data
pie_chart.add('A', 20.5)
pie_chart.add('B', 36.0)
pie_chart.add('C', 35.9)
pie_chart.add('D', 5.5)
pie_chart.add('E', 80.3)
# rendering the svg to file
pie_chart.render_to_file('donut_chart.svg')
    

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:

    pip install pygal_maps_world
    

Basic Example

      import pygal
# importing World_map package 
from pygal.maps.world import World
# creating object
worldmap_chart = pygal.maps.world.World()
# adding title
worldmap_chart.title = 'Some countries'
# add data
worldmap_chart.add('F countries', ['fr', 'fi'])
worldmap_chart.add('I countries', ['in', 'il','iq'])
worldmap_chart.add('M countries', ['ma', 'mc', 'md', 'me', 'mg',
                                   'mk', 'ml', 'mm', 'mn', 'mo',
                                   'mr', 'mt', 'mu', 'mv', 'mw',
                                   'mx', 'my', 'mz'])
worldmap_chart.add('U countries', ['ua', 'ug', 'us', 'uy', 'uz'])
# rendering the svg to file
worldmap_chart.render_to_file('basic_map.svg')
    

Adding Values to Map

      import pygal
# importing World_map package 
from pygal.maps.world import World
# creating the object
worldmap_chart = pygal.maps.world.World()
# adding the title.
worldmap_chart.title = 'random data'
# adding the random values.
worldmap_chart.add('values', {
  'af': 14,
  'bd': 1,
  'by': 3,
  'cn': 1000,
  'gm': 9,
  'in': 1,
  'ir': 314,
  'iq': 129,
  'jp': 7,
  'kp': 6,
  'pk': 1,
  'ps': 6,
  'sa': 79,
  'so': 6,
  'sd': 5,
  'tw': 6,
  'ae': 1,
  'us': 43,
  'ye': 28
})
# directly rendering the file to browser 
worldmap_chart.render_in_browser()
    

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

Continents Chart

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

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

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

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

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

  • Default

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

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

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

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

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

Parametric Style

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

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

There are five types of parametric styles in pygal.

  • Dark style

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

    • Saturate style

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

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

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

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

Custom Style

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

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

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.

Gaurav Singhal

Gaurav S.

Guarav is a Data Scientist with a strong background in computer science and mathematics. He has extensive research experience in data structures, statistical data analysis, and mathematical modeling. With a solid background in Web development he works with Python, JAVA, Django, HTML, Struts, Hibernate, Vaadin, Web Scrapping, Angular, and React. His data science skills include Python, Matplotlib, Tensorflows, Pandas, Numpy, Keras, CNN, ANN, NLP, Recommenders, Predictive analysis. He has built systems that have used both basic machine learning algorithms and complex deep neural network. He has worked in many data science projects, some of them are product recommendation, user sentiments, twitter bots, information retrieval, predictive analysis, data mining, image segmentation, SVMs, RandomForest etc.

More about this author