【发布时间】:2017-05-17 20:45:08
【问题描述】:
这里是菜鸟。我有这个脚本,它每 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()
【问题讨论】:
-
for plotinsideloop in range(300000):的这一行是什么? -
这就是如何让它循环很长时间以不断刷新情节
-
好的,我明白了。然后您需要将
data = list(reader(f))之后的代码缩进到与with open(newest) as f:相同的级别 -
请看我编辑的答案。
-
@GamerDude 欢迎来到 SO。请下次展示您为解决您面临的问题所做的一些工作,并说出您遇到的其他问题。此外,使您的代码尽可能简洁将使更多人花时间阅读它,了解错误并为您提供帮助。
标签: python csv matplotlib