Understanding your data with graphs

Lead Image © Oleksiy Tsupe, 123RF.com

Beautiful Data

One picture is worth a thousand words … or table columns … so learn how to create graphs on your Raspberry Pi and display them on your tablet or smartphone.

At SwitchDoc Labs, data often reveals itself better as a graph than as a table. When you show the data visually, it is much easier to pick out a pattern. This is especially true when you have a complex sensor project like Project Curacao, which now has 17 sensors, with five more to come. Project Curacao [1] is a solar- and wind-powered Raspberry Pi/Arduino project located on the island country of Curaçao.

One of the Raspberry Pi functions in Project Curacao is generating graphs. Yes, I could ship the data to another server and generate the graphs there, but seeing that I have a nice Linux platform on site, I decided to make the graphs there. The graphs then go to both to the Project Curacao Live webpage [2] and a directory on the Raspberry Pi for display on the iPad RasPiConnect [3] app.

In this month's column, I look at a fabulous (although somewhat difficult for a beginner) open source graphing program for Python called Matplotlib [4]. Matplotlib facilitates making publication-quality plots using methods similar to MATLAB, a commercial scientific programming language. You can output formats such as PDF, PostScript, SVG, and PNG. It's complicated, but very useful – especially with some examples.


Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications. The Pylab module is based on a state machine, designed to resemble closely that of MATLAB. Many people coming out of school are familiar with MATLAB and could make the transition to Matplotlib easily. Although you can run MATLAB on the Raspberry Pi, you would still need a PC to help it along [5].

Installing Matplotlib on the Raspberry Pi is a simple process, but understand that it will take about eight hours on a Rasp Pi 1. Start the long bits before you go to bed. The necessary packages for Matplotlib (Listing 1) and estimated times to completion are from a Wyolum posting [6]. Interestingly enough, I tested these instructions on my brand new Raspberry Pi 2 (RPi2), and I finished the sequence in less than 2.5 hours. Compare this with eight hours on a Raspberry Pi A+. More RAM and higher processor speed does make a big difference!

Listing 1

Installing Matplotlib Packages

$ sudo apt-get install libblas-dev ## 1-2 minutes
$ sudo apt-get install liblapack-dev ## 1-2 minutes
$ sudo apt-get install python-dev ## fast
$ sudo apt-get install libatlas-base-dev ## 1 hour
$ sudo apt-get install gfortran ## 2-3 minutes
$ sudo apt-get install python-setuptools ## fast
$ sudo easy_install scipy ## 5 hours (depends on Pi Model)
$ sudo apt-get install python-matplotlib ## 1 hour

Matplotlib has literally thousands of options [7]. The documentation goes on for pages and pages. I would suggest that you take one of the examples here (the line chart is a good meaty example) and read through and understand each line of the code. Then, just modify it to your needs. No matter what you are thinking of graphing, you can find a close example and model your own project on it. That's the magic of having a tool that thousands of people have used for their own projects.

The Python and Matplotlib source code for all the graphs used in Project Curacao are located on GitHub [8].

A First Simple Example

Listing 2 shows the code for a simple plot. Taking it step by step, you first import the pylab module of Matplotlib. Line 3 sets the minimum value, maximum value, and step value of the x axis. The arange() function is just like range(), but it generates an evenly spaced range of floating point numbers instead of integers. The statement in the next line generates sine data from the range of variable t derived in the previous step. The plot() function then plots t on the x versus s list data on the y axes. Finally, lines 7-9 create labels for the axes, line 10 puts a default grid on the graph, and line 11 saves the data into a file called test.png.

Listing 2

A Simple Graph

01 from pylab import *
03 t = arange(0.0, 2.0, 0.01)
04 s = sin(2*pi*t)
05 plot(t, s)
07 xlabel('time (s)')
08 ylabel('voltage (mV)')
09 title('About as simple as it gets, folks')
10 grid(True)
11 savefig("test.png")
12 show()

If your Pi has a graphical display, line 12 shows you the graph (Figure 1). You can do some pretty amazing charts, even in 3D [9], without writing much code at all (Figure 2), but you still have to generate the data!

Figure 1: A simple Matplotlib graph.
Figure 2: Examples of more complex Matplotlib graphs.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content