high_low_mean.py
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 22 18:19:57 2017
@author: dqhpl
"""
import csv
from matplotlib import pyplot as plt
from datetime import datetime#获取时间的包
filename = 'sitka_weather_2014.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
highs,dates, lows, means = [],[],[],[]
for row in reader:
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d")#转换为日期形式,格式记住
low = int(row[3])
high = int(row[1])
mean = int(row[2])
except ValueError:
print(current_date, 'missing data')#如果有日期缺失数据提醒没有数据
else:
dates.append(current_date)
lows.append(low)
highs.append(high)#每一行第1列的数据加入(这个其实是第2列,真正的第1列是第0列)
means.append(mean)
fig = plt.figure(dpi =128,figsize=(10,5))
plt.plot(dates, highs, c='red', alpha = 0.3)#alpha指定透明度,0是完全透明,1是完全不透明
plt.plot(dates, lows, c='blue', alpha =0.3)
plt.plot(dates, means, c='yellow')
plt.fill_between(dates, highs, lows, facecolor= 'blue', alpha=0.1)
#设置图像格式
plt.title("Daily high temperatures - 2014", fontsize = 14)
fig.autofmt_xdate()#绘制斜的日期标签
plt.ylabel("Temperature(F)", fontsize =10)
plt.savefig('temperature.png', bbox_inces = 'tight')
#plt.show()#plt.show()打开matplotlib查看器,并显示绘制的图形
# for index,column_header in enumerate(header_row):#enumerate()表示来获取每个元素的索引及其值
# print(index, column_header)


文章评论