График в реальном времени - PullRequest
0 голосов
/ 01 мая 2019

Я работаю с графиком «в реальном времени», который будет циклически повторяться каждые 10 секунд, измерять температуру из GPIO и добавлять эти данные к графику matplotlib с соответствующим временем.У меня есть код для этого, но когда я пытаюсь запустить его, max number of values для осей X вообще не работает, а значение datetime имеет значение bugs - иногда это показывает черные прямоугольники, иногда цифры, а когда это показываетчисла - это похоже на 10:50, 11:00, 11:50, 12:00, но я хочу измерение каждые 10 секунд, а не 50 минут, 10 минут и т. д. Я не знаю, какая часть кода неправильна, может кто-нибудь помочь мне, пожалуйста?Вот код



import datetime
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import RPi.GPIO as GPIO
import dht11
from datetime import datetime as dd
import time as tt
import matplotlib.dates as md
import dateutil

# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []

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

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



# This function is called periodically from FuncAnimation
def animate(i, xs, ys):

    # Read temperature (Celsius) from TMP102
    result = instance.read()
    temp_c = result.temperature
    # Add x and y to lists
    tem = tt.strftime("%H:%M", tt.localtime(tt.time()))
    dates = [dateutil.parser.parse(tem)]
    ax=plt.gca()
    ax.set_xticks(dates)
    xfmt = md.DateFormatter('%H:%M')
    ax.xaxis.set_major_formatter(xfmt)
    if result.is_valid():
        xs.append(dates)
        ys.append(temp_c)

    # Limit x and y lists to 5 items
        xs = xs[-5:]
        ys = ys[-5:]

    # Draw x and y lists
        ax.clear()
        ax.plot(xs, ys)

    # Format plot
        plt.xticks(rotation=45, ha='right')
        plt.subplots_adjust(bottom=0.30)
        plt.title('Temperature over Time')
        plt.ylabel('Temperature (deg C)')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()        

...