Python is full of libraries that can visualize data. One of the more interactive options comes from Pygal, which I consider the library for people who like things to look good. It generates beautiful SVG (Scalable Vector Graphics) files that users can interact with. SVG is a standard format for interactive graphics, and it can lead to rich user experiences with only a few lines of Python.

Shaun Taylor-Morgan

Using Pygal for stylish Python plots

In this introduction, we want to recreate this multi-bar plot, which represents the UK election results from 1966 to 2020:

Before we go further, note that you may need to tune your Python environment to get this code to run, including the following.

  • Running a recent version of Python (instructions for LinuxMac, and Windows)
  • Verify you’re running a version of Python that works with these libraries

The data is available online and can be imported using pandas:

import pandas as pd
df = pd.read_csv('https://anvil.works/blog/img/plotting-in-python/uk-election-results.csv')

Now we’re ready to go. The data looks like this:

        year  conservative  labour  liberal  others
0       1966           253     364       12       1
1       1970           330     287        6       7
2   Feb 1974           297     301       14      18
..       ...           ...     ...      ...     ...
12      2015           330     232        8      80
13      2017           317     262       12      59
14      2019           365     202       11      72

Read the full article here.