'''
要在文本文件中存储数据,最简单的方式是将数据作为一系列以逗号分隔的值(CSV)写入文件
数据来源:sitka_weather_07-2014.csv
'''
import csv
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
reader = csv.reader(f)#打开文件,并存储在列表中
header_row = next(reader)#返回文件的下一行
print(header_row)
['AKDT', 'Max TemperatureF', 'Mean TemperatureF', 'Min TemperatureF', 'Max Dew PointF', 'MeanDew PointF', 'Min DewpointF', 'Max Humidity', ' Mean Humidity', ' Min Humidity', ' Max Sea Level PressureIn', ' Mean Sea Level PressureIn', ' Min Sea Level PressureIn', ' Max VisibilityMiles', ' Mean VisibilityMiles', ' Min VisibilityMiles', ' Max Wind SpeedMPH', ' Mean Wind SpeedMPH', ' Max Gust SpeedMPH', 'PrecipitationIn', ' CloudCover', ' Events', ' WindDirDegrees']
for index,column_header in enumerate(header_row):#enumerate获取每个元素的索引及值
print(index,column_header)
0 AKDT
1 Max TemperatureF
2 Mean TemperatureF
3 Min TemperatureF
4 Max Dew PointF
5 MeanDew PointF
6 Min DewpointF
7 Max Humidity
8 Mean Humidity
9 Min Humidity
10 Max Sea Level PressureIn
11 Mean Sea Level PressureIn
12 Min Sea Level PressureIn
13 Max VisibilityMiles
14 Mean VisibilityMiles
15 Min VisibilityMiles
16 Max Wind SpeedMPH
17 Mean Wind SpeedMPH
18 Max Gust SpeedMPH
19 PrecipitationIn
20 CloudCover
21 Events
22 WindDirDegrees
#读取每天的最高气温
highs = []
for row in reader:
#使用int将字符串转为数字,让matplotlib能够读取
high = int(row[1])
highs.append(high)
print(highs)
[64, 71, 64, 59, 69, 62, 61, 55, 57, 61, 57, 59, 57, 61, 64, 61, 59, 63, 60, 57, 69, 63, 62, 59, 57, 57, 61, 59, 61, 61, 66]
#绘制气温图表
import matplotlib.pyplot as plt
fig = plt.figure(dpi = 128, figsize = (10,6))
plt.plot(highs, c = 'red')
plt.title('daily high temperates, july 2014',fontsize = 24)
plt.xlabel('', fontsize = 16)
plt.xlabel('temperates', fontsize = 16)
plt.tick_params(axis = 'both', which = 'major', labelsize = 16)
plt.show()
import csv
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
reader = csv.reader(f)#打开文件,并存储在列表中
header_row = next(reader)#返回文件的下一行
print(header_row)
###打印文件头及其位置
for index,column_header in enumerate(header_row):#enumerate获取每个元素的索引及值
print(index,column_header)
###提取并读取数据
#读取每天的最高气温,以及读取图表中日期
from datetime import datetime
dates, highs = [],[]
for row in reader:
#使用int将字符串转为数字,让matplotlib能够读取
high = int(row[1])
highs.append(high)
date = datetime.strptime(row[0], "%Y-%m-%d")
dates.append(date)
print(highs)
#绘制气温图表
import matplotlib.pyplot as plt
fig = plt.figure(dpi = 128, figsize = (10,6))
plt.plot(dates, highs, c = 'red')
plt.title('daily high temperates, july 2014',fontsize = 24)
plt.xlabel('', fontsize = 16)
fig.autofmt_xdate()#绘制斜的日期标签
plt.ylabel('temperates', fontsize = 16)
plt.tick_params(axis = 'both', which = 'major', labelsize = 16)
plt.show()
import csv
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
reader = csv.reader(f)#打开文件,并存储在列表中
header_row = next(reader)#返回文件的下一行
###提取并读取数据
#读取每天的最高气温,以及读取图表中日期
from datetime import datetime
dates, highs, lows = [],[],[]
for row in reader:
#使用int将字符串转为数字,让matplotlib能够读取
high = int(row[1])
highs.append(high)
low = int(row[3])
lows.append(low)
date = datetime.strptime(row[0], "%Y-%m-%d")
dates.append(date)
#绘制气温图表
import matplotlib.pyplot as plt
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.title('daily high temperates, july 2014',fontsize = 24)
plt.xlabel('', fontsize = 16)
fig.autofmt_xdate()#绘制斜的日期标签
plt.ylabel('temperates', fontsize = 16)
plt.fill_between(dates, highs, lows, facecolor = 'blue', alpha = 0.1)#fill_between填充颜色
plt.tick_params(axis = 'both', which = 'major', labelsize = 16)
plt.show()
页面更新:2024-04-30
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号