37,718
社区成员
发帖
与我相关
我的任务
分享
import csv
from datetime import datetime
from matplotlib import pyplot as plt
#from matplotlib.pyplot as plt
#从文件中获取日期和最高气温
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates,highs=[],[]
for row in reader:
current_date = datetime.strptime(row[0],"%Y-%m-%d")
dates.append(current_date)
high = int(row[1])
highs.append(high)
# dates = [datetime.strptime(row[0],"%Y-%m-%d") for row in reader]
# highs = [int(row[1]) for row in reader]
#该部分for循环为什么无法改成列表解析式,是我改错了吗(如果可以请告诉我列表解析式使用的条件)
#根据数据绘制图片
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red')
#设置图形格式
plt.title("Daily High Temperatures,July 2014",fontsize=24)
plt.xlabel('',fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature(F)",fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.show()