python编程从入门到实践16.1下载数据,csv格式,生成天气图表

https://blog.csdn.net/u014636245/article/details/82804251

pip install pygal

 

import csv
from matplotlib import pyplot as plt
from datetime import datetime
# filename = 'sitka_weather_2014.csv'
filename = 'death_valley_2014.csv'
with open(filename) as f:
	
	# ''' 分析CSV文件头,不太直观'''
	# reader = csv.reader(f)
	# header_row = next(reader) 
	# print(header_row)

	# ''' 打印文件头,显示直观'''
	# reader = csv.reader(f)
	# header_row = next(reader)
	# for index,column_header in enumerate(header_row):
	# 	print(index,column_header)
	
	# ''' 打印单个数据,列数据'''
	# reader = csv.reader(f)
	# highs = []
	# for row in reader:
	# 	highs.append(row[1])
	# print(highs)

	''' 打印单个数据,列数据,并且跳过首行'''
	reader = csv.reader(f)
	header_next_row = next(reader) 
	dates,highs,lows = [],[],[] 
	 
	for row in reader: 
		try:
			current_date = datetime.strptime(row[0],'%Y-%m-%d') 
			high = int(row[1]) 
			low = int(row[2]) 
		except ValueError:
			print(current_date,"Error")
		else: 
			dates.append(current_date) 
			highs.append(high) 
			lows.append(low) 

	fig = plt.figure(dpi = 256,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 = 1)
	plt.title("title",fontsize = 24)
	plt.xlabel("",fontsize = 8)
	fig.autofmt_xdate()
	plt.ylabel("F",fontsize = 16) 
	plt.tick_params(axis = 'both',which='major',labelsize = 8)
	plt.show() 

 

你可能感兴趣的:(python编程从入门到实践,python数据可视化)