Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Troy Kranendonk

Creating Interactive Charts with Python Pygal

Troy Kranendonk

  • Jan 10, 2019
  • 6 Min read
  • 23,579 Views
  • Jan 10, 2019
  • 6 Min read
  • 23,579 Views
Python

Introduction to Pygal

When it comes to interactive charts and the modules you can use, you may be familiar with Matplotlib, Seaborn, and Bokeh, which each allow interactivity and support additional features.

However, Pygal specializes in allowing the user to create SVGs. Besides the scalability of an SVG, you can edit them in any editor and print them in very high quality resolution. SVG formatting is easily integrated with Flask and Django as well. Lastly, you can easily and minimally create line charts, bar graphs, and Radar Charts (to name a few) with very little code, as we'll see shortly with the following examples:

description

description

description

Installation

Open Terminal

1$  pip install Pygal
bash

Extra Help: Installing and Dependencies - Link

Using Pygal

Step 1: Import

1import pygal
python

Step 2: Create a Variable to Store the Graph

For our first example we'll create a bar chart. We'll simply need to create a variable and store pygal.Bar() inside.

1import pygal
2b_chart = pygal.Bar()
python

You can easily use pygal.Line, pygal.pie, or any of the following.

Step 3: Add Some Values

Next we need to start creating our chart. I will be using Data that I scraped from a gaming tracker for the game Destiny 2. Eventually the graph will be live and I'll be able to see my stats change (hopefully) in real time.


description


Let's not get ahead of ourselves.

In short, everytime I play in a PvP or Crusible Match and eliminate an opponent or end up getting eliminated myself, the KD (Kill/Death Ratio) will change. I simply want to compare myself to my clan mates.

Thus, to start things off, we'll need a chart title.

1import pygal
2b_chart = pygal.Bar()
3b_chart.title = "Destiny Kill/Death Ratio"
python

Now we can start adding in our data. I need 3 bars, one for each player. To accomplish this, I'll need to use add followed by a title and some values.

1import pygal
2b_chart = pygal.Bar()
3b_chart.title = "Destiny Kill/Death Ratio"
4b_chart.add("Dijiphos", [0.94])
5b_chart.add("Punisherdonk", [1.05])
6b_chart.add("Musclemuffins20", [1.10])
python

Technically, we can finish and render without further customization. To render quickly to a browser, we'll use render_in_browser() as our output.

1import pygal
2b_chart = pygal.Bar()
3b_chart.title = "Destiny Kill/Death Ratio"
4b_chart.add("Dijiphos", [0.94])
5b_chart.add("Punisherdonk", [1.05])
6b_chart.add("Musclemuffins20", [1.10])
7b_chart.render_in_browser()
python

Customizing the Graph

Say we want some custom colors added to our graph #E80080 #E80080 #404040 #404040 #9bc850 #9BC850

This is easy to do, and can actually be achieved in multiple ways. First, import style from pygal.style.

1import pygal
2from pygal.style import Style
3
4b_chart = pygal.Bar()
5b_chart.title = "Destiny Kill/Death Ratio"
6b_chart.add("Dijiphos", [0.94])
7b_chart.add("Punisherdonk", [1.05])
8b_chart.add("Musclemuffins20", [1.10])
9b_chart.render_in_browser()
python

You can change a number of objects by simply adding:

1import pygal
2from pygal.style import Style
3custom_style = Style(
4
5b_chart = pygal.Bar()
6b_chart.title = "Destiny Kill/Death Ratio"
7b_chart.add("Dijiphos", [0.94])
8b_chart.add("Punisherdonk", [1.05])
9b_chart.add("Musclemuffins20", [1.10])
10b_chart.render_in_browser()
python

Notice I left the parentheses open.


Properties & Description

plot_backgroundThe color of the chart area background backgroundThe color of the image background foreground|The main foregrond color colorsThe serie color list value_colorsThe print_values color list Complete List: http://www.pygal.org/en/stable/documentation/custom_styles.html


Let's change the color of our bars by using the object colors.Don't forget to indent and close our final parentheses.

1import pygal
2from pygal.style import Style
3custom_style = Style(
4  colors=('#E80080', '#404040', '#9BC850'))
5
6b_chart = pygal.Bar()
7b_chart.title = "Destiny Kill/Death Ratio"
8b_chart.add("Dijiphos", [0.94])
9b_chart.add("Punisherdonk", [1.05])
10b_chart.add("Musclemuffins20", [1.10])
11b_chart.render_in_browser()
python

Now, all we need to do is pass style=custom_style in our pygal.Bar() to get it to work.

1import pygal
2from pygal.style import Style
3custom_style = Style(
4  colors=('#E80080', '#404040', '#9BC850'))
5
6b_chart = pygal.Bar(style=custom_style)
7b_chart.title = "Destiny Kill/Death Ratio"
8b_chart.add("Dijiphos", [0.94])
9b_chart.add("Punisherdonk", [1.05])
10b_chart.add("Musclemuffins20", [1.10])
11b_chart.render_in_browser()
python

description


That's a Wrap

It's that simple. I would check out Pygal's documentation for further builds. However, after reading this guide, you should have all the knowledge needed to create visually appealing graphs and charts with Python Pygal.


Thanks for reading this guide on using the Pygal module. Say hello on Twitter.