'''要在文本文件中存儲數據,最簡單的方式是将數據作為一系列以逗号分隔的值(CSV)寫入文件數據來源:sitka_weather_07-2014.csv'''
分析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()
image.png
在圖表中添加日期
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()
image.png
再繪制一個數據,給圖表區域着色
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()
,
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!