mport csv
from matplotlib import pyplot as plt
from datetime import datetime

# filename = 'sitka_weather_07-2014.csv'
# filename = 'sitka_weather_2014.csv'
filename = 'death_valley_2014.csv'
with open(filename) as f:
# 分析csv文件头
reader = csv.reader(f)
header_row = next(reader)
print(header_row)
# enumerate获取每个元素的索引及其值
# 打印头文件及其位置
for index, column_header in enumerate(header_row):
print(index, column_header)
# 提取并读取数据
highs = []
lows = []
dates = []
# 可能出现错误,对错误做处理
for row in reader:
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d")
high = int(row[1])
low = int(row[3])
except ValueError:
print(current_date,'missign date')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
# print(highs)
# 根据数据绘制图
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c="red")
plt.plot(dates, lows, c="blue")
# plt.title('Daily high temperatures,July 2014', fontsize=21)
plt.title('Daily high temperatures - 2014', fontsize=21)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate() # 绘制倾斜的X轴标签
plt.ylabel('Temperature (F)', fontsize=16)
# 给图表区域着色
plt.fill_between(dates, highs, lows, facecolor='yellow',alpha=0.1)
plt.tick_params(axis='both', which='major', labelsize=16)

plt.show()