如何告诉matplotlib不考虑除1到1000之外的任何数字?

时间:2017-05-17 20:45:09

标签: python csv matplotlib

Noob在这里。我有这个脚本,每5分钟从刷新的.csv文件绘制我的数据数据。问题是.csv数据有时会出现错误。也许是字母,也许是其他一些东西。如何告诉matplotlib不要考虑任何不是1到1000的数字?我会粘贴下面的代码。感谢。

import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot, dates
from matplotlib.dates import HourLocator, DateFormatter, DayLocator, 
YearLocator, MinuteLocator
from csv import reader
from dateutil import parser
import os
import time
import pylab
import datetime
from datetime import datetime, date
os.chdir('/home/pi/csvdata')

time.sleep(30)
def plotloop():
hours = (HourLocator())
minutes = (MinuteLocator())
days = (DayLocator())
dayFormatter = DateFormatter('%X %x')      # e.g., 12
for plotinsideloop in range(300000):
    dated_files = [(os.path.getmtime(fn), os.path.basename(fn)) 
        for fn in os.listdir("/home/pi/csvdata") if 
fn.lower().endswith('.csv')]
    dated_files.sort()
    dated_files.reverse()
    newest = dated_files[0][1]
    with open(newest) as f:
        data = list(reader(f))
    humidity = [i[1] for i in data]
    dates1 = [i[0] for i in data]
    dates = [datetime.strptime(i, '%X %x') for i in dates1]
    #print dates
    print dates1[0], dates1[-1]
    fig, ax = pyplot.subplots()
    fig.subplots_adjust(bottom=0.2)
    ax.xaxis.set_major_locator(days)
    ax.xaxis.set_minor_locator(hours)
    ax.xaxis.set_major_formatter(dayFormatter)
    firstdate = (dates[0])
    firstdate1 = str(firstdate)#[:10]
    print "_______"
    #print firstdate
    lastdate = (dates[-1])
    lastdate1 = str(lastdate)
    lastdate2 = lastdate1.replace(" ", " ")
    firstdate2 = firstdate1.replace(" ", " ")
    lastdate3 = lastdate2.replace(":", " ")
    firstdate3 = firstdate2.replace(":", " ")
    lastdate4 = lastdate3.replace("-", " ")
    firstdate4 = firstdate3.replace("-", " ")
    lastdate5 = lastdate4.split(" ")
    firstdate5 = firstdate4.split(" ")
    print lastdate4
    print firstdate4
    firstdate6 = map(int, firstdate5)
    lastdate6 = map(int, lastdate5)
    #lastdate6 = [int(z) for z in lastdate5]
    #firstdate6 = [int(v) for v in firstdate5]
#    firstdatey = int.firstdate4[0]
##        firstdatem
##        firstdated
##        firstdateh
##        firstdatemin
##        firstdatesec

    print lastdate6
    print firstdate6
    titlename = (firstdate1, " - ", lastdate1)
    print titlename
    #print lastdate
    ax.set_xlim(datetime (*firstdate6), datetime (*lastdate6))
    pyplot.ylim(10,50) 
    ax.xaxis_date()
    ax.autoscale_view()
    pyplot.setp(pyplot.gca().get_xticklabels(), rotation=45, 
horizontalalignment='right')
    pyplot.xticks(rotation=15)
    pyplot.plot_date(dates, humidity)
    pyplot.title(titlename)
    pyplot.savefig(newest + '_2.png', dpi=260)
    pyplot.savefig("plot_2.png", dpi=260)
    #pyplot.savefig("test.pdf")
    print ("Done")
    print(lastdate)
    time.sleep(300)



plotloop()

这里有更新的代码:

import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot, dates
from matplotlib.dates import HourLocator, DateFormatter, DayLocator, YearLocator, MinuteLocator
from csv import reader
from dateutil import parser
import os
import time
import pylab
import datetime
from datetime import datetime, date
os.chdir('/home/pi/csvdata')

#time.sleep(30)
def plotloop():
        hours = (HourLocator())
        minutes = (MinuteLocator())
        days = (DayLocator())
        dayFormatter = DateFormatter('%X %x')      # e.g., 12
        for plotinsideloop in range(300000):
                dated_files = [(os.path.getmtime(fn), os.path.basename(fn)) 
                        for fn in os.listdir("/home/pi/csvdata") if fn.lower().endswith('.csv')]
                dated_files.sort()
                dated_files.reverse()
                newest = dated_files[0][1]
                with open(newest) as f:
                        data = list(reader(f))

        humidity = [i[1] for i in data]
        dates1 = [i[0] for i in data]
        humdates = zip(humidity,dates1)
        humdatesfiltered = []
        for humdate in humdates:
            try:
                if 1 <= humdate[0] <= 1000:
                    humdatesfiltered.append(humdate)
            except TypeError:
                pass 

        dates = [datetime.strptime(i, '%X %x') for i in dates1]
        #print dates
        print dates1[0], dates1[-1]
        fig, ax = pyplot.subplots()
        fig.subplots_adjust(bottom=0.2)
        ax.xaxis.set_major_locator(days)
        ax.xaxis.set_minor_locator(hours)
        ax.xaxis.set_major_formatter(dayFormatter)
        firstdate = dates[0]
        lastdate = dates[-1]
        print "_______"

        titlename = (firstdate1.strftime('%Y-%m-%d'), " - ", lastdate1.strftime('%Y-%m-%d'))
        print titlename

        ax.set_xlim(firstdate1, lastdate1)
        pyplot.ylim(10,50) 
        ax.xaxis_date()
        ax.autoscale_view()
        pyplot.setp(pyplot.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
        pyplot.xticks(rotation=15)
        pyplot.plot_date([i[1] for i in humdatesfiltered], 
                         [i[0] for i in humdatesfiltered])
        pyplot.title(titlename)
        pyplot.savefig(newest + '_2.png', dpi=260)
        pyplot.savefig("plot_2.png", dpi=260)
        #pyplot.savefig("test.pdf")
        print "Done"
        print lastdate
       #       time.sleep(300)




plotloop()

2 个答案:

答案 0 :(得分:0)

一个问题是,您希望删除这些有问题的值,但您无法从其中一个列表推导中删除它们,因为这样您的两个列表将具有不同数量的元素。

你可以做的是zip()将两个列表放在一起然后删除有问题的值。即使湿度列表中有字母,下面也会这样做。

def plotloop():
    hours = (HourLocator())
    minutes = (MinuteLocator())
    days = (DayLocator())
    dayFormatter = DateFormatter('%X %x')      # e.g., 12
    for plotinsideloop in range(300000):
        dated_files = [(os.path.getmtime(fn), os.path.basename(fn)) 
                for fn in os.listdir("/home/pi/csvdata") if fn.lower().endswith('.csv')]
        dated_files.sort()
        dated_files.reverse()
        newest = dated_files[0][1]
        with open(newest) as f:
                data = list(reader(f))

        humidity = [i[1] for i in data]
        dates1 = [i[0] for i in data]
        humdates = zip(humidity,dates1)
        humdatesfiltered = []
        for humdate in humdates:
            try:
                if 1 <= humdate[0] <= 1000:
                    humdatesfiltered.append(humdate)
            except TypeError:
                pass 

        dates = [datetime.strptime(i, '%X %x') for i in dates1]
        #print dates
        print dates1[0], dates1[-1]
        fig, ax = pyplot.subplots()
        fig.subplots_adjust(bottom=0.2)
        ax.xaxis.set_major_locator(days)
        ax.xaxis.set_minor_locator(hours)
        ax.xaxis.set_major_formatter(dayFormatter)
        firstdate = dates[0]
        lastdate = dates[-1]
        print "_______"

        titlename = (firstdate1.strftime('%Y-%m-%d'), " - ", lastdate1.strftime('%Y-%m-%d'))
        print titlename

        ax.set_xlim(firstdate1, lastdate1)
        pyplot.ylim(10,50) 
        ax.xaxis_date()
        ax.autoscale_view()
        pyplot.setp(pyplot.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
        pyplot.xticks(rotation=15)
        pyplot.plot_date([i[1] for i in humdatesfiltered], 
                         [i[0] for i in humdatesfiltered])
        pyplot.title(titlename)
        pyplot.savefig(newest + '_2.png', dpi=260)
        pyplot.savefig("plot_2.png", dpi=260)
        #pyplot.savefig("test.pdf")
        print "Done"
        print lastdate
        #       time.sleep(300)    

plotloop()

答案 1 :(得分:0)

这是一个可能更容易使用的重组版本:

import csv
import datetime
import glob
import matplotlib
matplotlib.use('Agg')    # has to be called before pyplot is imported
import matplotlib.pyplot as plt
from matplotlib.dates import HourLocator, DateFormatter, DayLocator, YearLocator
import os
# import pylab
from time import sleep

CSV_DIR       = "/home/pi/csvdata"
DATE_FORMAT   = "%X %x"         # ex "07:06:05 09/30/13"
DAY_FORMAT    = "%Y-%m-%d"      # ex "2013-09-30"
INITIAL_DELAY = 30
REDRAW_DELAY  = 300

DEBUG = True
if DEBUG:
    def debug_print(s):
        print(s)
else:
    def debug_print(s):
        pass

def wait(i):
    debug_print("Waiting for {} seconds".format(i))
    sleep(i)

def get_newest_file(dir, ext=None):
    debug_print("Finding newest file")
    if ext is None:
        # no ext given - match all file extensions
        filespec = "*"
    elif ext[:1] == ".":
        # ext starts with period - don't repeat it
        filespec = "*" + ext
    else:
        # no period - insert one
        filespec = "*." + ext

    # make full search path
    path = os.path.join(dir, filespec)
    # get all matching files
    file_names = glob.glob(path)

    if not file_names:
        # no matching files found
        debug_print("  nothing found")
        return None
    else:
        # find newest file
        newest = max(file_names, key = os.path.getmtime)
        debug_print("  found {}".format(newest))
        return newest

def get_humidity_data(csv_fname):
    hum = []
    dat = []
    parsetime = datetime.datetime.strptime
    debug_print("Reading data")
    good, skip = 0, 0
    with open(csv_fname, "r") as csv_file:
        for row in csv.reader(csv_file):
            try:
                h = int(row[1])
                d = parsetime(row[0], DATE_FORMAT)
                if 1 <= h <= 1000:
                    hum.append(h)
                    dat.append(d)
                    good += 1
                else:
                    skip += 1
            except ValueError:
                skip += 1
    debug_print("  found {} good rows, skipped {} bad rows".format(good, skip))
    return hum, dat

def make_graph(humidities, dates_):
    firstdate = dates_[0]
    lastdate = dates_[-1]
    graph_title = "{} - {}".format(firstdate.strftime(DAY_FORMAT), lastdate.strftime(DAY_FORMAT))
    debug_print("Making graph for {}".format(graph_title))

    # create new plot
    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom = 0.2)
    # configure axes
    ax.xaxis_date()
    ax.xaxis.set_major_locator(DayLocator())
    ax.xaxis.set_minor_locator(HourLocator())
    ax.xaxis.set_major_formatter(DateFormatter(DATE_FORMAT))
    ax.set_xlim(firstdate, lastdate)
    ax.set_ylim(10,50)
    ax.autoscale_view()
    plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
    plt.xticks(rotation=15)
    # add data
    plt.plot_date(dates_, humidities)
    plt.title(graph_title)
    return fig

def plot_humidity():
    newest = get_newest_file(CSV_DIR, "csv")
    if newest is None:
        debug_print("No data file found!")
    else:
        h, d = get_humidity_data(newest)
        fig = make_graph(h, d)
        fig.savefig(newest + '_2.png', dpi=260)
        # fig.savefig("plot_2.png", dpi=260)
        # pyplot.savefig("test.pdf")
        debug_print("Done")

def main():
    wait(INITIAL_DELAY)
    while True:
        plot_humidity()
        wait(REDRAW_DELAY)

if __name__=="__main__":
    main()