python读取csv文件(python数据可视化--CSV文件格式)

生活 科技 更新时间: 2024-07-07 10:09:12

'''要在文本文件中存储数据,最简单的方式是将数据作为一系列以逗号分隔的值(CSV)写入文件数据来源:sitka_weather_07-2014.csv'''

分析csv文件头

import csvfilename = '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 AKDT1 Max TemperatureF2 Mean TemperatureF3 Min TemperatureF4 Max Dew PointF5 MeanDew PointF6 Min DewpointF7 Max Humidity8 Mean Humidity9 Min Humidity10 Max Sea Level PressureIn11 Mean Sea Level PressureIn12 Min Sea Level PressureIn13 Max VisibilityMiles14 Mean VisibilityMiles15 Min VisibilityMiles16 Max Wind SpeedMPH17 Mean Wind SpeedMPH18 Max Gust SpeedMPH19 PrecipitationIn20 CloudCover21 Events22 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()

python读取csv文件(python数据可视化--CSV文件格式)1

image.png

在图表中添加日期

import csvfilename = '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()

python读取csv文件(python数据可视化--CSV文件格式)2

image.png

再绘制一个数据,给图表区域着色

import csvfilename = '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()

python读取csv文件(python数据可视化--CSV文件格式)3

,