Drawing and Displaying Graphs on Raspberry Pi and RasPiConnect using MatPlotLib
We often have data that is better shown as a graph rather than a table. It is much easier to pick out a pattern when you show the data visually. This is especially true when you have a complex sensor project like Project Curacao (There are 17 sensors on the project and We are about to add 5 more). Project Curacao is a solar powered Raspberry Pi / Arduino project located down on the island of Curacao.
One of the things we use the Raspberry Pi for on Project Curacao for is to generate graphs. Yes, we know we could ship the data to another server and generate the graphs there, but since we have a nice linux platform, we decided to make the graphs on site. Then we ship the graphs both to a webpage and also over to the RasPiConnect directory for display on the RasPiConnect App.
MatPlotLib is a python library for making publication quality plots using methods similar to MATLIB. You can output formats such as PDF, Postscript, SVG, and PNG.
Here’s a picture of the embedded graph (in RasPiConnect):
This particular graph shows the actual installation of the box in Curacao. Note how the solar power takes off when it is put up in the sun. Next we show the MatPlotLib code to generate a graph, and then show how to put it on a RasPiConnect screen.
Example MatPlotLib Code
Here is the example matplotlib code and graph for one of the graphs (including the call to MySQL to get the data). I am using the Solar/Wind line to show which power source is selected. Reg Wind Volt gives the voltage on the regulated side of the DC/DC 12V – 6V Buck converter (fed to the solar power charging circuitry) and Unreg Wind Volt is on the Wind Turbine side of the converter. We choose to use just points because of the intermittent nature of the wind. See the post on the wind storm we had a couple of weeks ago. This was the graph generated after a couple of trips in the car with various wind turbines.
# solar wind graph generation
# filename: solarwindgraph.py
# Version 1.4 01/12/14
#
# contains graphing routines
#
#
import sys
import time
import RPi.GPIO as GPIO
import gc
import datetime
import matplotlib
# Force matplotlib to not use any Xwindows backend.
Note that if you want to use a button to select different type of graphs for the same RasPiConnect control, you write the graph name to a file and the read the file to get what graph to display. Make sure you assign the Web Control to refresh when you push the button. Then you get an immediate response.
You can select a different graph by using a feedback control button to write that file (that contains the graph name) and cycle through graphs (see my video on this blog for an example) as below:
# FB-11 – change the graph display
if (objectServerID == “FB-11”):
#check for validate request
# validate allows RasPiConnect to verify this object is here
12 Trackbacks / Pingbacks
Comments are closed.