处理连续温度数据,并绘制图片(含命令行接口定义方法)python

po上一个以前写的,没有po上来的脚本,主要是怕以后找不到了QAQ

import sys,re,argparse
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument('--inputfile',type = str)
parser.add_argument('--parament',type = str)
namespace = parser.parse_args()
environment = namespace.parament

f = open(namespace.inputfile,"r")
title=f.readline().split()
for i in range(len(title)):
        if title[i]==environment:
                environment=i+2
def get_colunm_pyre(f,xlist,split_flag=None): #get the col
        line=[]
        for i in f:
                i=i.strip("\n")
                m=i.split(split_flag)
                #print (m[0],len(m))
                for k in range(len(xlist)):
                        try:
                                line.append(m[xlist[k]-1])
                        except:
                                print(m[0])
                                break
                #line.append("\n")
        return line

def match_by_str(f,str_match): #get the row
        pattern=re.compile(str_match)
        line=[]
        for i in f:
                #if str_match in i:
                m=pattern.search(i)
                while m:
                        line.append(i)
                        m=False
        return line

f = open(namespace.inputfile,"r")	#open the file 
#get the col 
xlist=[8,environment]
line = get_colunm_pyre(f,xlist,"\t")
f.close()

#display the data

draw={}
for i in range(0,len(line),2):
        if line[i+1] != "None":
                draw[line[i]]=line[i+1]
        else:
                continue

time = list(draw.keys())
#print(len(draw))
data = list(draw.values())
data_int = []
for t in data:
        data_int.append(float(t))
data = data_int
avg = sum(data)/len(data)
for t in time:
        if float(draw[t])>=2*avg:
                del draw[t]
day = []
time = []
time = list(draw.keys())
for i in time:
        a = i.split(",")
        day.append(a[0])
day=list(set(day))
day.sort()
#print(day)

low={}
high={}
for i in day:
        num=[]
        line = match_by_str(time,i)
        #print(line)
        for k in line:
                num.append(float(draw[k]))
        #print(min(num))
        low[i] = min(num)
        high[i] = max(num)

draw_y1=[]
draw_y2=[]
draw_x=day
for i in day:
        draw_y1.append(low[i])
        draw_y2.append(high[i])
'''
def adjust_list(draw_y1):
        for i in range(len(draw_y1)):
                if draw_y1[i]>=2*(sum(draw_y1)/len(draw_y1)):
                        draw_y1[i] = sum(draw_y1)/len(draw_y1)
        return(draw_y1)
draw_y1 = adjust_list(draw_y1)
draw_y2 = adjust_list(draw_y2)
'''
f.close()
print(draw_y1,"\n",draw_y2)
##draw
fig = plt.figure(figsize=(10,6))
plt.plot(draw_x,draw_y1,c='blue',alpha=0.5)
plt.plot(draw_x,draw_y2,c='red',alpha=0.5)
#plt.fill_between(low.keys(),high.values(),low.values(),facecolor='orange',alpha=0.3)

#title = title1[parament-1]
plt.title("{},{}:{}".format(title[environment-2],title[20],title[21]),fontsize=24)
plt.xlabel('Dates',fontsize=14)
#绘制倾斜的日期标签
fig.autofmt_xdate()
plt.ylabel('Temperature(℃)',fontsize=14)
#设置坐标轴参数
plt.tick_params(axis='both',which='major',labelsize=10)

plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值