0

I'm trying to make a real-time ploting graph for my raspberry pi temperature sensor. I would like to make a measure every for example 20 min and insert data to graph with that time at bottom.

I have this code, but the time is in string format and matplot won't let me insert it to ylim because it wants a int what should I do please?


import matplotlib.pyplot as plt
import RPi.GPIO as GPIO
import dht11
import time
from drawnow import drawnow


# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 4
instance = dht11.DHT11(pin=4)


def make_fig():
    plt.plot(x, y) 

plt.ion() 
fig = plt.figure() 

x = list()
y = list()

while True:
    result = instance.read()
    tem = time.strftime("%H:%M:%S", time.localtime(time.time()))
    x.append(tem)
    y.append(result.temperature)  
    drawnow(make_fig)
    time.sleep(1000)


1 Answers1

0

A quick search took me to this question which I think is actually what you are asking for:

https://stackoverflow.com/a/9627970/8275139

Don't look the answer marked as correct, the one below is a better and more efficient solution.

Use datetime instead of time to parse your dates and add them to your x list as you are doing. Then import matplotlib.dates lib and use plt.gca().xaxis.set_major_formatter() to let matplotlib know the correct format for that dates.

Luiscri
  • 913
  • 1
  • 13
  • 40