Plotting live data

Besides plotting data from files, we can use matplotlib to plot sensor data as it is sampled. To achieve this, we can use the plot-animation feature, which automatically calls a function to collect new data and update our plot.

Create the following script, called live_graph.py:

#!/usr/bin/python3 #live_graph.py import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation import data_local as dataDevice PADDING=5 myData = dataDevice.device() dispdata = [] timeplot=0 fig, ax = plt.subplots() line, = ax.plot(dispdata) def update(data): global dispdata,timeplot timeplot+=1 dispdata.append(data) ax.set_xlim(0, timeplot) ymin = min(dispdata)-PADDING ymax = max(dispdata)+PADDING ax.set_ylim(ymin, ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.