Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.

Python Plotting problem

I am having problems with the following code, running on a Raspberry pi 3
I can plot the graphs individually so I know the hardware is working.
When I run this code I see four graphs but nothing is plotted.

Code:
Code: Select all | TOGGLE FULL SIZE
`from gpiozero import CPUTemperaturefrom time import sleep, strftime, timeimport matplotlib.pyplot as pltimport osimport busioimport digitalioimport boardimport adafruit_mcp3xxx.mcp3008 as MCPfrom adafruit_mcp3xxx.analog_in import AnalogIn# create the spi busspi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)# create the cs (chip select)cs = digitalio.DigitalInOut(board.D22)# create the mcp objectmcp = MCP.MCP3008(spi, cs)# create cpu objectcpu = CPUTemperature()# Initialise the subplot function using number of rows and columnsfigure, axis = plt.subplots(2, 2)# create an analog input channel on pin 0chan0 = AnalogIn(mcp, MCP.P0)while True:    x= time()          # plot temperature    temp = cpu.temperature    axis[0,0].plot(x,temp)    plt.draw()    print ('Core Temperature:' + str(temp))    plt.pause(1)    # plot +5V    adc=chan0.voltage    axis[0,1].plot(x,chan0.voltage)    plt.draw()    print('ADC Voltage: ' + str(chan0.voltage) + 'V')    plt.pause(1) `

BillPretty

Posts: 21
Joined: Sun Mar 07, 2021 2:44 pm

Re: Python Plotting problem

Well folks I got the software working.
The hardware uses a two 4.99K 1% resistors as a voltage divider, to measure the 5 volt input.
The user can easily use the same type of divider to measure the 3.3 volt rail.

The working code is shown below:

Code:

#sudo apt-get install python3-matplotlib
#sudo apt-get install python3-gi-cairo

from gpiozero import CPUTemperature
from time import sleep, strftime, time
import matplotlib.pyplot as plt
import os
import busio
import digitalio
import board

# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D22)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# create cpu object
cpu = CPUTemperature()

# Initialise the subplot function using number of rows and columns
figure, axs = plt.subplots(2)

plt.ion()

x1 = []
x2 = []

y1 = []
y2 = []

while True:

# plot temperature
temp = cpu.temperature
y1.append(temp)
x1.append(time())
axs[0].set_title("Core Temperature")
axs[0].plot(x1,y1)
print ('Core Temperature:' + str(temp))

# plot +5V
# Get the ADC measurement X2 for voltage divider