16 下载数据
16.1 CSV文件格式
16.1.1 分析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)
复制代码
16.1.2 打印文件头及其位置
- import csv
- filename ='sitka_weather_07-2014.csv'
- with open(filename) as f:
- reader = csv.reader(f)
- header_row = next(reader)
-
- for index,cloum_header in enumerate(header_row):
- print(index,cloum_header)
复制代码
16.3.1 提取并读取数据
- import csv
- # 从文件中获取最高气温
- filename = 'sitka_weather_07-2014.csv'
- with open(filename) as f:
- reader = csv.reader(f)
- header_row = next(reader)
-
- highs = []
- for row in reader:
- highs.append(row[1])
-
- print(highs)
复制代码- import csv
- #从文件中获取最高气温
- filename = 'sitka_weather_07-2014.csv'
- with open(filename) as f:
- reader = csv.reader(f)
- header_row = next(reader)
-
- highs = []
- for row in reader:
- high = int(row[1])
- highs.append(high)
-
- print(highs)
复制代码
16.1.4 绘制气温图标
- import csv
- from matplotlib import pyplot as plt
- # 从文件中获取最高气温
- filename = 'sitka_weather_07-2014.csv'
- with open(filename) as f:
- reader = csv.reader(f)
- header_row = next(reader)
-
- highs = []
- for row in reader:
- high = int(row[1])
- highs.append(high)
-
- # 根据数据绘制图形
- fig = plt.figure(dpi=128,figsize=(10,6))
- plt.plot(highs,c='red')
- #设置图形的格式
- plt.title("Daily high temperatures, July 2014",fontsize = 24)
- plt.xlabel('',fontsize=16)
- plt.ylabel("Temperature (F)", fontsize=16)
- plt.tick_params(axis='both', which='major', labelsize=16)
- plt.show()
复制代码
16.1.5模块datetime
- from datetime import datetime
- first_date = datetime.strptime('2014-7-1','%Y-%m-%d')
- print(first_date)
复制代码
16.1.6 在图表中添加日期
- import csv
- from datetime import datetime
- from matplotlib import 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)
-
- # 根据数据绘制图形
- 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()
复制代码
16.1.7覆盖更长的时间
- import csv
- from datetime import datetime
- from matplotlib import pyplot as plt
- # 从文件中获取日期和最高气温
- filename = 'sitka_weather_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)
-
- # 根据数据绘制图形
- fig = plt.figure(dpi=128,figsize=(10,6))
- plt.plot(dates,highs,c='red')
- plt.title("Daily high temperatures - 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()
复制代码
16.1.8 再绘制一个数据系列
- import csv
- from datetime import datetime
- from matplotlib import pyplot as plt
- # 从文件中获取日期和最高气温
- filename = 'sitka_weather_2014.csv'
- with open(filename) as f:
- reader = csv.reader(f)
- header_row = next(reader)
-
- dates, highs, lows = [], [], []
- for row in reader:
- current_date = datetime.strptime(row[0],"%Y-%m-%d")
- dates.append(current_date)
-
- high = int(row[1])
- highs.append(high)
-
- low = int(row[3])
- lows.append(low)
-
- # 根据数据绘制图形
- fig = plt.figure(dpi=128,figsize=(10,6))
- plt.plot(dates,highs,c='red')
- plt.plot(dates,lows,c='blue')
- plt.title("Daily high temperatures - 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()
复制代码
16.1.9 给图片区域着色
- import csv
- from datetime import datetime
- from matplotlib import pyplot as plt
- # 从文件中获取日期和最高气温
- filename = 'sitka_weather_2014.csv'
- with open(filename) as f:
- reader = csv.reader(f)
- header_row = next(reader)
-
- dates, highs, lows = [], [], []
- for row in reader:
- current_date = datetime.strptime(row[0],"%Y-%m-%d")
- dates.append(current_date)
-
- high = int(row[1])
- highs.append(high)
-
- low = int(row[3])
- lows.append(low)
-
- # 根据数据绘制图形
- 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.1)
- plt.title("Daily high temperatures - 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()
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |