Skip to Content
Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
book

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

by Steven Lawrence Fernandes, Tim Cox
April 2018
Beginner content levelBeginner
552 pages
13h 58m
English
Packt Publishing
Content preview from Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Raspberry Pi for Python Programmers Cookbook - Second Edition

Raspberry Pi for Python Programmers Cookbook - Second Edition

Tim Cox

Publisher Resources

ISBN: 9781788629874Supplemental Content