Sunday, March 29, 2015

Display Raspberry Pi CPU temperature graphically, using Python 2 with Matplotlib and drawnow

Python 2 example to display Raspberry Pi CPU temperature graphically, with Matplotlib and drawnow.



This exercise work on Python 2 and need matplotlib and drawnow, refer to "Install numpy, matplotlib and drawnow for Python 2".

plotTemp.py
import os
import matplotlib.pyplot as plt
from drawnow import *

tempC = []

plt.ion()
cnt=0

def plotTempC():
    plt.ylim(20,80)
    plt.title('Raspberry Pi core temperture')
    plt.grid(True)
    plt.ylabel('Temp C')
    plt.plot(tempC, 'rx-', label='Degrees C')
    plt.legend(loc='upper right')

#pre-load dummy data
for i in range(0,26):
    tempC.append(0)
    
while True:

    ostemp = os.popen('vcgencmd measure_temp').readline()
    temp = (ostemp.replace("temp=", "").replace("'C\n", ""))
    print(temp)
    tempC.append(temp)
    tempC.pop(0)
    drawnow(plotTempC)
    plt.pause(.5)





For Python 3:

Matplotlib for Python 3 is not support currently. I tried to build it from source (refer "Install numpy, matplotlib and drawnow for Python 2" and "Fail to build matplotlib for Python3, run on Raspberry Pi 2"), but nothing plotted while running.



Similar example without using drawnow, "Plot RPi 2 core temperature using Python 2 and matplotlib.pyplot".

3 comments:

Bob Leinweber said...

hit a problem:
-------------------------------
Traceback (most recent call last):
File "/home/pi/plotTemp.py", line 31, in
drawnow(plotTempC)
File "/usr/local/lib/python2.7/dist-packages/drawnow/drawnow.py", line 61, in drawnow
else: plt.draw_all()
AttributeError: 'module' object has no attribute 'draw_all'
---------------
not sure why this happened but sure would like to make these graphs work.
Any ideas? Python2.7 -Jessie Raspberry pi2b

Brett Bolen said...

same. ubuntu 16.04 lts.

Andr.oid Eric said...

hello Bob Leinweber and Brett Bolen,
may be you installed a old/wrong version of drawnow.