简体   繁体   English

在 python 中绘制绘图

[英]plotting with drawnow in python

I am trying to monitor sensor data in real-time, but the plot shows nothing, below is just an example.我正在尝试实时监控传感器数据,但 plot 什么也没显示,下面只是一个示例。 can anyone explain to me how come the result is showing nothing?谁能向我解释为什么结果什么也没显示?

在此处输入图像描述

import datetime
import random
import matplotlib.pyplot as plt
from drawnow import *
from matplotlib.dates import AutoDateLocator, AutoDateFormatter, date2num

i = 0
x = 0
y = 0
FirstTime = str('00:00')
LastTime = str('00:00')

def CreatePlot():
    figure = plt.subplot()
    plt.plot([],[])
    date_datetime = datetime.datetime.strptime(LastTime, '%H:%M')
    int_date = date2num( date_datetime)
    locator = AutoDateLocator()
    figure.xaxis.set_major_locator(locator)
    figure.xaxis.set_major_formatter( AutoDateFormatter(locator) )
    min_date = date2num( datetime.datetime.strptime(FirstTime, '%H:%M') )
    max_date = date2num( datetime.datetime.strptime(LastTime, '%H:%M') )
    plt.xlim(min_date, max_date)
    plt.plot(x,y, 'r-')
    plt.gcf().autofmt_xdate()

while True:
    x = datetime.datetime.now() + datetime.timedelta(minutes=i)
    x = datetime.datetime.strftime(x,'%H:%M')
    if i == 0:
        FirstTime = x
    else:
        LastTime = x
    y = (2*i)+2
    if i>500:
        break
    else:
        drawnow(CreatePlot)
        plt.pause(0.0001)
        i+=1

I solved the issue, so I am gonna explain it to help someone else like me, the first issue is changing date format to string with strftime, plotting string in x-axis is not auto-formattable, also following commands are redundant:我解决了这个问题,所以我将解释它以帮助像我这样的其他人,第一个问题是使用 strftime 将日期格式更改为字符串,在 x 轴上绘制字符串不是自动格式化的,以下命令也是多余的:

min_date = date2num( datetime.datetime.strptime(FirstTime, '%H:%M') )
max_date = date2num( datetime.datetime.strptime(LastTime, '%H:%M') )
plt.xlim(min_date, max_date)

in addition, to make a better view someone can add the following commands too:此外,为了更好地查看,也可以添加以下命令:

from matplotlib.ticker import AutoMinorLocator
from matplotlib.dates import AutoDateLocator, AutoDateFormatter
.
.
.
.
ax0 = plt.subplot(2,2,1)
locator = AutoDateLocator()
ax0.xaxis.set_major_locator(locator)
formatter = AutoDateFormatter(locator)
ax0.xaxis.set_major_formatter(formatter)
ax0.xaxis.set_minor_locator(AutoMinorLocator())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM