Wednesday, April 8, 2015

Plot RPi 2 core temperature using Python 2 and matplotlib.pyplot

Similar to the previous example "Display Raspberry Pi CPU temperature graphically, using Python 2 with Matplotlib and drawnow", but use library of matplotlib.pyplot only, no drawnow.


import os
import matplotlib.pyplot as plt

tempC = []
plt.ion()
cnt=0

def plotNow():
    plt.clf()
    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')
    plt.plot(tempC)
    plt.show()

#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)
    plotNow()
    plt.pause(.5)



No comments: