#coding:utf-8
import csv
from matplotlib import pyplot as plt
from datetime import datetime
# ~ first_date = datetime.strptime('2014-7-1', '%Y-%m-%d') #打印日期
# ~ print(first_date)
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f: #使用with 会在不用文件时,自动关闭文件
dates, highs, lows = [], [], []
reader = csv.reader(f) #创建一个新的reader对象
header_row = next(reader) #返回文件的下一行
# ~ print(header_row)
# ~ header_row = next(reader) #返回文件的下一行
# ~ print(header_row)
# ~ for index, column_header in enumerate(header_row): #获取索性及其值
# ~ print(index, column_header)
for row in reader: #获取第一列数据
#获取最高温度
high = int(row[1])
highs.append(high)
#获取最低气温
low = int(row[3])
lows.append(low)
#获取日期
current_date = datetime.strptime(row[0], "%Y-%m-%d")
dates.append(current_date)
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha=0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.4) #在区间填充颜色
#设置图形的格式
plt.ylim([45, 72]) #设置y坐标的取值范围
plt.title("Daily high and low temperatures-July 2014", fontsize=24)
plt.xlabel('', fontsize=16)
plt.ylabel("Temperature (F)", fontsize=16) #给y轴加标签
fig.autofmt_xdate() #使x轴标签倾斜
plt.tick_params(axis='both', which='major', labelsize=16) #修改x轴和y轴的刻度
plt.show()
需要导入一个csv文件