1 month ago

Plot Sense HAT data with matplotlib with Dashboard Pi

Creating a plot with data obtained by the Sense HAT can help to make that data more understandable. In this tutorial we look at how to Plot Sense HAT data with matplotlib.

Using Python together with the powerful matplotlib library, we can obtain and plot data from the Sense HAT sensors.

If we have a web server running, we can create a virtual host where the plots can be stored and accessed over the network. This is known as a dashboard. There are existing services that would allow us to do this, but while most of them require a subscription, this one is free.

For details on how to create a dashboard for Sense HAT data, check out page 46 of The MagPi #46. The previous version of this project was focused on the use of temperature and humidity sensors, probably the easiest Sense HAT data to understand. This time, we are going to take advantage of the Sense HAT’s powerful motion sensors.

We will use the matplotlib Python library to create two graphs: a 3D scatter plot using the XYZ position of the Sense HAT, and a 2D line plot using the angular velocity data.

Plot Sense HAT data: What you need:

Setting up the web server

With a dynamic DNS service, a web server and a virtual host, you can create your own dashboard. While these three items are not needed to create the plots, without them it would be only possible to track the results locally.

The first step is to create a folder called dashboard in /var/www. We will use this folder to save the CSV file generated by the program, along with all the data, and the HTML or PHP files, and use them to create a beautiful dashboard.

There are plenty of tutorials available online to show you how to design your dashboard, so we won’t cover this here. When you have designed your dashboard, create a folder called images in /var/www/dashboard.

Importing libraries

Our code needs to import two libraries: csv in order to write the data to a file, and matplotlib to create the plots. After importing the libraries, we need to use the matplotlib.use(‘Agg’) command to allow the program to work without a running X server.

We also need to import matplotlib.pyplot in order to draw the plot, matplotlib.ticker to correctly format the axis, and mpl_toolkits in order to create the 3D plot. We need NumPy to work with the lists, and plot SenseHAT data from the Sense HAT. Finally, our code imports the time module in order to add a time.sleep to the while loop. This should have a value of a second or less if we want to get a realistic impression of the Sense HAT’s movements from the motion sensors. The value of the time.sleep cannot be zero, as the angular velocity is calculated as displacement/time, and we cannot divide our displacement value by zero.

Manipulating data

Matplotlib uses lists to plot Sense HAT data, so we need to create some empty lists. Our code will then take the data from the Sense HAT and add it to the lists. The code calculates the angular displacement, and then divides this value by the time in order to calculate the angular velocity. All this data is appended to a CSV file in case we want to check it later, or manipulate and analyse it using other hardware or software.

Plot Sense HAT data

Creating the plot

We could take the data from the CSV file and create the plots, but matplotlib can do this easily using the lists we have created. Our code starts the 3D plot, then calculates the minimum and maximum values for the lists using NumPy. This information is used to format the axes before cleaning and saving the plot.

A similar procedure is used to plot the angular velocity against the data count. In this case we are creating a simple line plot. Our code defines the limits, using NumPy again to calculate the minimum and maximum values to be plotted. For this graph there is no need to format the axes, but we need to label them to make the plot easy to interpret. Finally, the code cleans and saves the plot.

The last step, just before ending the while loop, is to add the time.sleep, with a value greater than zero. If we want to produce a useful plot, this value should be close to 1 second or lower. The time.sleep value is set as a variable called snooze, just before the start of the while loop.
Using the data we have collected, we can track how the orientation of the Sense HAT has changed, and analyse the angular velocity of these changes.

This code is only an example of the things you could do using the Sense HAT data. You could create amazing projects using it as a base for your own code. You could create different plots, and use the measured data to correct the orientation of a rover or drone. You could then monitor the plots to double-check the resulting behaviour. You don’t even need a Sense HAT – any sensors could be used, just by changing their references in the code.

Plot Sense HAT data

And that’s how you plot Sense HAT data.

Matplotlib was created by John D Hunter. He passed away in 2012. Everything your author has created using his amazing library, including this tutorial, is offered in his memory.

This article was written by Iker García. Born and raised in the Basque Country, Iker is studying for a PhD in Chemical Engineering. His hobby is programming, and learning about different devices.