Data Visualization in Python: Introduction to Pygal

Riya Mistry
datascience
Published in
5 min readApr 8, 2020

Visualization is very efficient mean for conveying information. As you might have heard ‘A picture is worth a thousand word’s, similar relationship exists between graphs and data. One area where visualization is extremely useful is “Exploratory Data Analysis”.

What is Pygal?

Pygal is python based data visualization library. It was built by a tech company in France. It makes use of popular SVG (Scalable Vector graphics) format, where digital images are defined as a sequence of vector statements. For e.g. a line can be represented as sequence of points connected together rather than collection of pixel with rgb values. SVG images are defined as markup that is rendered, usually in browser using tags and attributes. This format is great for interactivity, indexing and searching. Also defining objects as vector retains sharpness regardless of resizing or screen resolution. Thus it is a great library for building graphs that are visually appealing.

What’s so special about Pygal?

Interactivity in visualization is main thing that is lacking in many other visualization tools. Interactive graphs facilitates data exploration by allowing you to filter out certain elements in your plot, but this ability to zoom in on specific elements and zoom out to view a bigger picture dramatically increases understanding of the data that you have. This is where pygal comes into picture.

Pygal visualization library are optimized for creating and working with SVG images, with rich support for interactivity and animation.

As a data visualization library, pygal can be compared with other visualization tools available in market like matplotlib, Seaborn, Bokeh, Plotly.py, etc.

While this may seem a large number of tools for same task but each library has it’s own niche.

→ Pygal’s niche is working with SVG images.

Now since you have basic idea about what Pygal is, let’s move towards setting up the environment for working with it.

Installation:

let us install the pygal library. Open the console and write the following command.

If you work with Jupyter Notebook then you may open the conda terminal and enter the command as follows:

Simple, isn’t it? Once the setup is complete you are now ready to create any charts you want. Here, in this tutorial we will look at some basic chart

  1. Line Graph
  2. Bar Charts
  3. Pie Charts

Line Graph:

Firstly you need to import the pygal library, then to draw a line you need to create a Line() object. The title is set with the use of title parameter.

render_to_file() function creates the svg graph and stores it with the image name specified in its parameter. By opening this image in browser, it will look like this:

As you can see currently mouse is pointing at location (9,9) of C line which is highlighted by the cursor. Also if you want to view on C line you can disable remaining line from there by disabling from legend.

see the legends A,B,D are disabled

Horizontal line:

To create horizontal line the only change you need to do is,

keeping the remaining code same, we will get the graph like:

horizontal line

Similarly, to get stacked line graph instead of pygal.Line(), create an object of pygal.StackedLine(fill=True)

Stacked line

Bar Graphs

example:

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.

output:

Stacked Bar Graph:

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

Output:

Horizontal Multiple Series Bar Chart:

output:

Pie Chart

We will now look at the example of creating a pie chart.

Donut:

It can easily be created by specifying inner_radius property in Pie object.

This was the basic introduction on how to work with pygal library. For more details visit the documentation site.

Conclusion:

In this tutorial, we learnt about what pygal is, its benefits and further practiced with some of its functions. Well this is not the end, in the next tutorial we will see how to read data from csv file, how to plot graph using it and finally embedding it in the flask web application.

Thank you for viewing this article!! Kindly share your reviews for the same whether it was helpful for you or not in the comments.

--

--