BreakingExpress

Style your information plots in Python with Pygal

Python is filled with libraries that may visualize information. One of the extra interactive choices comes from Pygal, which I contemplate the library for individuals who like issues to look good. It generates stunning SVG (Scalable Vector Graphics) recordsdata that customers can work together with. SVG is a regular format for interactive graphics, and it may well result in wealthy consumer experiences with just a few strains of Python.

Using Pygal for fashionable Python plots

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

Before we go additional, be aware that you could be have to tune your Python setting to get this code to run, together with the next. 

  • Running a latest model of Python (directions for LinuxMac, and Windows)
  • Verify you are working a model of Python that works with these libraries

The information is out there on-line and may be imported utilizing pandas:

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

Now we’re able to go. The information seems like this:

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

 

Plotting this in Pygal builds up in a approach that I discover straightforward to learn. First, we outline the fashion object in a approach that may simplify our bar chart definition. Then we go the customized fashion together with different metadata to a Bar object:

import pygal
from pygal.fashion import Style

custom_style = Style(
    colours=('#0343df', '#e50000', '#ffff14', '#929591'),
    font_family='Roboto,Helvetica,Arial,sans-serif',
    background='clear',
    label_font_size=14,
)

c = pygal.Bar(
    title="UK Election Results",
    fashion=custom_style,
    y_title='Seats',
    width=1200,
    x_label_rotation=270,
)

Then, we add our information into the Bar object:

c.add('Conservative', df['conservative'])
c.add('Labour', df['labour'])
c.add('Liberal', df['liberal'])
c.add('Others', df['others'])

c.x_labels = df['12 months']

Finally, we save the plot as an SVG file:

c.render_to_file('pygal.svg')

The result’s an interactive SVG plot you possibly can see on this gif:

Beautifully easy, and with stunning outcomes.

Conclusion

Some plotting choices in Python require constructing each object in nice element, and Pygal provides you that performance from the beginning. Give Pygal a go when you’ve got information available and also you need to make a clear, stunning, and easy plot for consumer interplay.

This article was first shared here and is edited and republished with permission.

Exit mobile version