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 Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

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 CPUTemperature
from time import sleep, strftime, time
import matplotlib.pyplot as plt
import os
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP

# 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, axis = plt.subplots(2, 2)

# create an analog input channel on pin 0
chan0 = 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
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
#sudo pip3 install adafruit-circuitpython-mcp3xxx

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

# 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)

# create an analog input channel on pin 0
chan0 = AnalogIn(mcp, MCP.P0)

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