【问题标题】:Plot in real time with date实时绘制日期
【发布时间】:2019-04-27 08:41:12
【问题描述】:

我正在尝试为我的树莓派温度传感器制作实时绘图图。我想每隔例如 20 分钟进行一次测量,并将数据插入到底部的图形中。

我有这段代码,但time 是字符串格式,matplot 不允许我将它插入到ylim,因为它需要int,请问我该怎么办?


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)


【问题讨论】:

    标签: python python-3.x matplotlib time


    【解决方案1】:

    快速搜索让我想到了这个问题,我认为这实际上是您所要求的:

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

    不要看标记为正确的答案,下面是一个更好更有效的解决方案。

    使用datetime 而不是time 来解析您的日期并将它们添加到您的x 列表中。然后导入matplotlib.dates lib 并使用plt.gca().xaxis.set_major_formatter() 让matplotlib 知道该日期的正确格式。

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2019-05-18
      • 2019-12-14
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多