The Data Science Maker: Live plotting data with Matplotlib and Raspberry Pi

Preface: This is my first post in a series of posts titled “The Data Science Maker” where I focus on the integration of data science software skills with consumer hardware for creative applications. The series is meant for those new or newly interested in electronics hardware, programming, and data science. Conversations in the comments are encouraged.

Note: I begin with a personal anecdote about my own experience as new electronics maker. To find the “How To” section skip to 1.

“Maybe it’s a concept album?”

My journey into the electronics maker realm is quite short at barely a year long, but there have already been some memorable events that an impartial outsider could reasonably describe as “mundane” where my brain would describe them as “first-trip-to-the-planetarium-amazing”. One such moment occurred earlier this year the first time I saw live temperature data via Matplotlib plot on a soon-to-be-broken 7" touchscreen that was controlled by a RaspberryPi 4. While not exactly on the same level of technological accomplishment as say, hearing Neil Armstrong’s voice crackle back from the surface of the moon, I was still filled with an astonishing amount of joy watching temperates show up in a Matplotlib figure every second, the shape of the lineplot jumping up and down as my thumb moved on and off of the sensor. Despite using electronic sensors my entire life in commercial products, seeing lines and lights go up and down, blue to red and back to blue, I had never built a device that made or presented those measurements. I had never coded electronics of any kind.

“This is amazing!” I said to no one in particular as a flood of ideas came into my head.

“Where’s the dog? George?? Nora! …Nora, have you seen George?!? I want to take his paw temperature!”

Why the paw? I don’t know. Maybe because he likes to raise it up to be held. Or perhaps because I thought it would be warm like my hand and not thermally insulated. You know, like a dog’s paw usually is.

No matter, we had found the dog!

“Okie, Georgie, paw!”

And it was at the moment… George realized he was not getting a treat, pulled his paw back, and dropped into play pose, prepped for a game I was definitely not going to play. No matter, I didn’t actually have a plan for what I would do with said paw temperature data, but I just felt like I had to poke some real, tangible thing with this temperature sensor sitting atop this very prototype-y looking breakout board. I had done it, I had made a thing! A thing that worked and did stuff! A real life thing that could be touched! And programmed! And tell me about the physical world by doing things with electrons and bits!

It was thrilling. It was fun. It was an early step along my path to not just manipulating and modeling the world as a data scientist, but engineering how I get data from the world.

About an hour later when George got the actual “paw” response he was looking for.

How To

  • Hardware Used: MacBook Pro (Late 2016), Raspberry Pi 4B (8GB), tmp102 tmp102 Qwiic board [1](no soldering required) or tmp102 board with soldering pads [2], Qwiic connect cable (Optional setup with soldering: Dupont cables (4 connections), soldering iron and solder, headers (4)
  • Software Used: Python (v3.8.5), Visual Studio Code (v1.54.3), Thonny (v3.3, Raspberry Pi)
  • Python Modules: csv, time, tmp102, matplotlib, pandas

The example code presented today was tested using the hardware listed at the top of this article and written using Python v3.8.5. Be aware that as you scale down from a single-board computer like the Raspberry Pi 4 to microcontrollers like Teensy or other microcontrollers from SparkFun and Adafruit you may end using MicroPython or CircuitPython, which are essentially scaled-down versions of full Python. I was able to code everything I needed and run the code from Thonny, Visual Studio Code, and the Raspberry Pi terminal. The screenshots were taken of my Raspberry Pi desktop using a VNC program (Virtual Network Computing) on my MacBook Pro that I will have a separate tutorial on in a later post. But don’t worry, everything can be done entirely on the Raspberry Pi in Thonny or terminal.

To get started, I recommend following the SparkFun guide [3–5] for a tmp102 breakout board with Qwiic connectors. This setup does not require soldering [2] and is the simplest starting point. Follow the guide and once your Raspberry Pi is set up for I2C connection you will be ready to start taking temperature measurements. The follow guide contains code I have modified from SparkFun to suit my own preferences. Experiment and see what best suits your own needs.

For the supplied code, I have used screenshots from VSCode as I prefer the syntax highlighting over Thonny. The actual code is found in GitHub gist links at the end of the article in section 7 as well as in the captions.

1) Live plotting of temperature data using matplotlib.animate

  • This program will measure and plot temperature in real time. This does not record or write the data. It only plots it as it is read from the sensor.
(1/2) Code for plotting temperature in real time. Code found at Github gist link
(2/2) Code for plotting temperature in real time. Code found at GitHub gist link
Screengrab showing the terminal after executing the program and the plot that automatically pops up. Once you run the program you don’t have to do anything but wait a second or two and the plot will appear. Note: results are plotted here in degrees Fahrenheit, but this can be changed by a simple conversion in the Python file.

2) Live plotting and writing data to csv

  • This program will measure temperature in real time and write the measurements to a csv file. Be aware that the output csv will be overwritten every time you run the program unless you change the file output name on line 16.
tmp_write code can be found at Github gist link
Terminal window showing temperature measurements measured and being written in real time (units: samples, temp in Celsius). tmp102_write code can be found at GitHub gist link

3) Reading csv data and live plotting it

  • This program will read csv temperature in real time and plot in real time. Be aware that the output csv will be overwritten every time you run the program unless you change the file output name on line 16.
(1/2) tmp102_read code can be found at GitHub gist link
(2/2) tmp102_read code can be found at GitHub gist link
Two programs are being run simultaneously in different terminal windows. The left is the tmp102_write program showing temperature measurements measured and being written in real time (units: samples, temp in Celsius). The tmp102_read program was executed in the right terminal window and the results are show in

4) Running multiple programs using a single temperature sensor

Just to see if it was possible, I decided to run multiple programs that access the temperature sensor at once. In the figure below, you can see the temperature sensor data being plotted without writing to a csv (right). Data is presented in degrees Fahrenheit and the temporal resolution is high (i.e., smaller periods of time between temperature measurements). In the leftmost window, data is being written to a csv from the temperature sensor using a slower sampling interval and plotted using the same time step (center). The two plots therefore sample from different sources, use different temperature scales, as well as sampling intervals. All of this to demonstrate how the temperature sensor can data can be split simultaneously into different ‘pipelines’ for different purposes. What interesting things can be made from splitting signals remains to be seen.

Rapidly plotting temperature without writing to csv (right). Writing temperature data to csv (leftmost terminal window) and plotting data using this time step (left Figure 1).

5) Wrap Up

I hope this article serves as a helpful showcasing of some of the possibilities single-board computers hold for the Data Science Maker. Using just the Python skills I had already honed from my data science bootcamp and a careful reading of online tutorials I made my first measurements of the physical world. A new experience I hope grows into some creative projects.

Don’t worry if you’re struggling to get the system working. While now I can log in and get temperature measurements remotely from my Mac via VNC Viewer with just a few clicks and lines of code this was not the case when I demoed this project live. This is rarely a great idea, but I decided to try anyways. I ended picking up the temperature sensor to show my classmates the hardware via the webcamera and accidentally pulled out a wire so the first half of the presentation I had faulty temperature readings. Another type of lesson learned that day.

I’ll be updating this post and adding to the series throughout 2021. I’m looking forward to the journey and reading comments and questions.

6) References

Accessed date format: (DD/MM/YYYY)

[1] Qwiic connect version of the tmp102 breakout board. This version does not require soldering.

[2] I used the SparkFun one and that is the only version I can confirm will run properly from this tutorial. Use other branded versions at your own discretion. (But if you do use another version and get it working please share in the comments)

[3] SparkFun tmp102 Digital Temperature Sensor Hookup Guide for breakout board with Qwiic connectors.

[4], SparkFun tmp102 Digital Temperature Sensor Hookup Guide for breakout board without Qwiic connect ports. This guide has links to a tutorials on breadboards, I2C, and Arduino Library’s among other useful links. Accessed (26/02/2021)

[5] This tutorial has variations on graphing live in Matplotlib and is the main source I derived my code from. Accessed (26/02/2021)

[6], Texas Instruments datasheet on tmp102 temperature sensor. Note: The SparkFun breakout board datasheet links to this datasheet because this is the sensor they use for their branded product. Accessed (26/02/2021)

7) Code

[i] tmp102_live This code plots live temperature data, but does not record or write data.

[ii] tmp102_write This code writes temperature data to a csv but does not plot it.

[iii] tmp102_read This code plots temperature data read in from the csv using a defined sampling step.

PhD Geologist/Geochemist, Farmer, and Musician turned Data Scientist. Electronics hobbyist.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store