每日最高/最低/平均值 [英] Max/Min/Mean Per Day

查看:142
本文介绍了每日最高/最低/平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我的txt文件的片段(data.txt)



Hi,

This is a snippet of my txt file (data.txt)

TimeStamp,Irradiance,Ambient_Temperature
21/7/2014 0:00,0.66,29.16
21/7/2014 0:00,0.71,29.16
21/7/2014 0:00,0.65,29.17
21/7/2014 0:00,0.67,29.17
21/7/2014 0:01,0.58,29.17
.
.
.
22/7/2014 23:58,0.69,28.54
22/7/2014 23:58,0.61,28.54
22/7/2014 23:58,0.65,28.51
22/7/2014 23:58,0.59,28.54
22/7/2014 23:59,0.63,28.55
22/7/2014 23:59,0.67,28.54
22/7/2014 23:59,0.68,28.56
22/7/2014 23:59,0.58,28.55







我的程序将要求用户输入从什么时间到什么时间,并将绘制辐照度和放大倍数。环境温度



现在,我可以显示超过天数的最大/最小/平均值

但我想要实现的是:



1.显示辐照度和光度的每日最大/最小值环境温度(每24小时内)

2.显示每天上午8:00至下午5:00之间的辐照度和平均值。环境温度



这是我的代码片段(用于显示最大/最小/平均值):






My Program will ask the user to enter from what time to what and will plot both the irradiance & ambient temperature

For now, i am able to display max/min/mean for over the days
but what i trying to achieve is:

1. Display the max/min per day for irradiance & ambient temperature(within each 24 hours period)
2. Display mean per day between 8:00a.m to 5:00p.m for irradiance & ambient temperature

This is a snippet of my code (for displaying max/min/mean):

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np # support large, multi-dimen arrays n matrices
from scipy.stats import mode # stats like finding mode
import re # regular expression (process text)
import itertools

linematchregex = re.compile('(\d+/\d+/\d+ \d+:\d+),(\d+\.\d+)') # find multiple pairs on the same line

startTime = datetime.strptime(raw_input('please enter start time in format like 21/7/2014 0:00 :'), '%d/%m/%Y %H:%M')
endTime   = datetime.strptime(raw_input('please enter end time in format like 22/7/2014 23:57 :') , '%d/%m/%Y %H:%M')

with open(r'temp.txt', 'r') as f:
f.next() #skip first line
t,temp = zip(*[p for line in f for p in linematchregex.findall(line)]) 
groups = itertools.groupby(f, lambda row: row[0].split(',')[0])
for date, group in groups:     
group = [(datetime.datetime.strptime(dt), value)                  
    for dt, value in group]      
during_8to5 = [(dt, value) for dt, value in group                          
  if datetime.time(8) <= dt.time() < datetime.time(17)]          


t = [datetime.strptime(x, '%d/%m/%Y %H:%M') for x in t ]
temp = [float(x) for i,x in enumerate(temp) if startTime<=t[i]<=endTime]

t = [x for x in t if startTime<=x<=endTime]
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
ax1.set_ylim(15,40)
ax1.plot(t, temp, 'c', linewidth=3.3)


short_title = ('Ambient Temperature vs. Time Graph \n From {} To {} \n ')
long_title = ('\n Max AMB_TEMP Value: {:.2f} at {} , Min AMB_TEMP Value: {:.2f} at {}, Mean AMB_TEMP Value: {:.2f} at {}\n')
fig.suptitle(short_title.format(startTime, endTime), fontsize=20, color='blue')
ax1.set_title(long_title.format(max(group),min(group),sum(during_8to5)/len(during_8to5),fontsize=16, color='green', ha='bottom'))
plt.ylabel(u'Ambient Temperature(\u2103)', fontsize=16, color='blue')
plt.xlabel('Time ($H:M$)', fontsize=16, color='blue')
fig.tight_layout()
plt.show()





我遇到的问题是:错误会说出日期,group in groups:line's程序中出现错误:意外缩进



Problem i have when i run it is: a error will say the for date, group in groups: line's there's an error in your program: unexpected indent

推荐答案

H:M


)',fontsize = 16,color ='blue')
fig.tight_layout()
plt.show()
)', fontsize=16, color='blue') fig.tight_layout() plt.show()





我运行时遇到的问题是:错误会说出日期,组中的组:行中的程序中有错误:意外缩进



Problem i have when i run it is: a error will say the for date, group in groups: line's there's an error in your program: unexpected indent


错误告诉你问题是什么是,即存在缩进错误。如您所知使用python,您需要确保代码结构在每个点都具有正确的缩进级别,否则它不是同步正确的。



从构建开始编译代码并以小块执行,直到抛出错误,告诉你哪个部分导致了问题,然后你可以看看那个部分并用缩进添加/删除空格等,直到问题得到解决。 />


The error is telling you what the problem is, i.e. there is an indentation error. As you know working with python, you need to ensure that the code structure has the correct level of indentation at each point otherwise it is not synatically correct.

Start by building up the code and execute in small chunks until the error is thrown that will tell you which part is causing the problem, you can then look at that section and add/remove spaces with etc. with the indents until the problem is fixed.

for date, group in groups:
group = [(datetime.datetime.strptime(dt), value)
    for dt, value in group]
during_8to5 = [(dt, value) for dt, value in group
  if datetime.time(8) <= dt.time() < datetime.time(17)]





快速浏览一下,建议这个看起来不对。



A quick glance, suggests this doesn't look right.


这篇关于每日最高/最低/平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆