#分析CSV文件头:
import csv
#添加日期
from datetime import datetime
#绘制温度图表
import matplotlib.pyplot as plt
filename = 'data/xxx.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
#从文件中获取日期和最高温度
dates,highs,lows = [],[],[]
for index,column_header in enumerate(header_row): #获取索引和值
print(index,column_header)
#提取并读取数据
highs = [ ]
for row in reader:
current_date = datetime.strptime(row[2],'%Y-%m-%d')
high = int(row(5))
low = int(row(6))
dates.append(current_date)
highs.append(high)
lows.append(low)
#根据最高温度绘制图形
plt.style.use('seaborn')
fig,ax = plt.subplots()
ax.plot(dates,highs,c = 'red',alpha = 0.5) #alpha 0 表示完全透明
ax.plot(dates,lows,c = 'blue',alpha = 0.5)
ax.fill_between(dates,highs,lows,facecolor = 'blue',alpha = 0.1) #接受一个x,两个y
#ax.plot(highs,c = 'red')
#设置图形样式
ax.set_title("标题",fontsize = 24)
ax.set_xlabel('',fontsize = 16)
#绘制倾斜的标签
fig.autofmt_xdate()
ax.set_ylabel("温度",fontsize = 16)
ax.tick_params(axis = 'both',which = 'major',labelsize = 16)
plt.show()