Chapter 3. Getting Started with pygal

In this chapter, we will start with building some basic SVG charts using the pygal charting library for Python and look at common chart types and styles.

Why use pygal?

In the world of Python development, there are many libraries for charts (Matplotlib and Plotly being a few examples), and cookbook-style books have been written for many of them. Since this is an introduction to data charting, we need a simple, easy-to-use library where developers new to Python charting, or Python charting in general, could easily take code from this book and build Python applications. The following screenshot shows the pygal website with some chart examples:

Why use pygal?

This is where pygal comes in; pygal (http://pygal.org/) is a Python-based SVG Charts Creator, developed by the Kozea Community (http://community.kozea.fr/), as shown in the following screenshot. This is a group dedicated to building quality open source libraries (mainly Python based, but for HTML5 projects as well).

Why use pygal?

The pygal library offers multiple charting options beyond what I consider standard charts: bar charts, line charts, and pie graphs. It includes a world map, funnel charts, radar charts, and box plots, to name just a few.

It also includes prebuilt themes and styles, which you do not have to customize if you are not inclined to do so. Also, since the chart library's output is SVG, this makes it a highly flexible output type for HTML5 or even print media. One issue with some charting libraries in Python is that the output defaults to the PNG format with a specified image size. Since SVG is a vector graphic (a type of graphic that is scalable without losing image quality), it can be scaled and resized for any need without loss of quality.

Take a look at the following screenshot of the documentation page for http://pygal.org/:

Why use pygal?

The pygal website also includes pretty good and easy-to-read documentation. One thing that's quite common with third-party Python libraries is that the documentation can range from a well-documented, online-searchable wiki to a simple readme.txt file that only shows how to install the library. The pygal library also doesn't require a lot of dependencies, which is crucial for an introductory book, as a very dependent library might cause issues for new developers or developers who are new to pygal.

Many Python frameworks have some very picky dependencies that you might need for your project, but they might or might not work with your system.

Why use pygal?

The lxml library is the only library required for pygal, but it has a few issues depending on which operating system you are running your Python code on. I encourage you to reread the notes on lxml (specifically if you're running Windows) before we cover the installation of pygal.

With that covered, let's install pygal and build some charts!

Installing pygal using pip

First and foremost, if you haven't installed lxml, if you're working on Windows, you'll want to install the lxml installer, as mentioned in Chapter 1, Setting Up Your Development Environment; otherwise, the following commands should install lxml for you. Next, we will use pip and install pygal using the following commands for Windows and Mac/Linux systems (note that sudo is used in the Mac and Ubuntu install).

If you are a Windows user, type the following command:

pip install pygal

If you are a Mac or Ubuntu user, type the following command:

sudo pip install pygal

Next, open Eclipse with PyDev and create a new Python project, followed by a new file (the settings aren't important since this is a test project). Once the project is created, create the new file, call it importtest.py, and type the following:

import pygal

If successful, you should be able to press Ctrl + Space bar and see PyDev's code hinting pull all of the libraries installed on the system. In the following screenshot, you can see pygal being recognized in my system:

Installing pygal using pip

Installing pygal using Python Tools for Visual Studio

If you plan on working in Visual Studio for the remainder of the book, here's a note on installation: first, if you haven't already installed lxml, as noted in Chapter 1, Setting Up Your Development Environment, then run easy_install with your Python environment in the Install Python Package window, as shown in the following screenshot:

Installing pygal using Python Tools for Visual Studio

If successful, your Solution Explorer window should look like what is shown in the following screenshot with lxml included:

Installing pygal using Python Tools for Visual Studio

Lastly, install the pygal library. Right-click on your environments and select Install Python Package, this time with pygal, as shown in the following screenshot:

Installing pygal using Python Tools for Visual Studio

Building a line chart

Line charts typically show how particular data changes at different intervals of time. In charting, this is the simplest chart that you can make, typically with the x and y axes and each axis on the chart indicating time, value, or another parameter.

Let's build a simple chart, in this case, on how many hits a website has received in the past two years (2012–2014). Take a look at the first line in the following code; this is a declarative line by the Python interpreter to specify the type of string encoding to the file. Also, you'll notice on line.x_labels that we use an inline function called range(). This lets us create an array of numbers, starting from the lowest number to the highest number; in the following case, 2012 and 2014 would print as 2012, 2013, 2014 in an array. Now, copy the following code into your project's main Python file:

# -*- coding: utf-8 -*-
import pygal

#create a new line chart.
line = pygal.Line()
line.title = 'Website hits in the past 2 years' #set chart title
line.x_labels = map(str, range(2012, 2014)) #set the x-axis labels.
line.add('Page views', [None, 0, 12, 32, 72, 148]) #set values.
line.render_to_file('linechart.svg') #set filename.

The following screenshot shows a basic pygal line chart output:

Building a line chart

In your main project file where you ran your script, you can see the linechart.svg file created. Open it, and your chart will look like what's shown in the preceding screenshot. To find the file, open the directory your project is in and find the linechart.svg file. Note that you can hover over the dots and get the values of each marker in the chart; these are some of the functionalities that come prebuilt with the pygal library.

We will also see that the chart's timeline starts from 0.0 on 2013. If you take a look at the line.add() statement, the first parameter is None; this adds a spacer in our chart to push the chart data out a little bit rather than forcing the chart to start at 2012. This is a common trick to setting up chart layouts.

Another feature is that if you hover over the line label (in this case, Page views) the entire line will be highlighted, indicating which dataset you're viewing with that label. The pygal library will also review your data and emphasize certain lines on the data axis, such as 0.0, 50.0, and 100.0, to break up some of the chart lines for easier readability.

Tip

Code hinting support for the appearance of the line() function for pygal depends on the IDE you are using. The pygal library is written in a slightly unusual way when compared to most Python libraries. The library generates each chart type dynamically using a for loop, which checks each chart class in the pygal library. Due to this, IDEs that require static, hardcoded functions in Python will throw an error, but not break when they are run. In other words, using code hinting might or might not work well depending on the editor you're using.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141