《Python编程从入门到实践 第二版》第十六章练习

16-1 锡特卡的降雨量 锡特卡属于温带雨林,降水量非常丰富。在数据文件sitka_weather_2018_simple.csv中,文件头PRCP表示的是每日降水量。请对这列数据进行可视化。如果你想知道沙漠的降水量有多低,可针对死亡谷完成同样的练习。

import csv

import matplotlib.pyplot as plt

from datetime import datetime

filename = 'D:/Sublime Text/python_test/16/sitka_weather_2018_simple.csv'

with open(filename) as f:
	reader = csv.reader(f)
	header_row = next(reader)

	# 从文件中获取每日降水量
	prcps = []
	for row in reader:
		prcp = float(row[3])
		prcps.append(prcp)

# 根据每日降水量绘制图形
plt.style.use('seaborn')
fig,ax = plt.subplots()
ax.plot(prcps,c='red')

# 设置图形格式
ax.set_title('锡特卡的降雨量',fontsize=24)
ax.set_xlabel('',fontsize=16)
ax.set_ylabel('PRCP',fontsize=16)

# 显示表格
plt.show()

16-2 比较锡特卡和死亡谷的温度 在有关锡特卡和死亡谷的图表中,温度刻度反映了数据范围的不同。为准确比较锡特卡和死亡谷的温度范围,需要在y轴上使用相同的刻度。
为此,请修改图16-5和图16-6所示图表的y轴设置,对锡特卡和死亡谷的温度范围进行直接比较(也可对任何两个地方的温度范围进行比较)。

import csv 

import matplotlib.pyplot as plt

from datetime import datetime

def weather_data(filename,dates,highs,lows,t_date,t_high,t_low):
	
	with open(filename) as f:
		reader = csv.reader(f)
		header_row = next(reader)

		# 从文件中获取日期、最高温度和最低温度
		for row in reader:
			current_date = datetime.strptime(row[t_date],'%Y-%m-%d')
			try:
				high = int(row[t_high])
				low = int(row[t_low])
			except ValueError:
				print(f'Missing data for {
     current_date}')
			else:
				dates.append(current_date)
				highs.append(high)
				lows.append(low)


# 获得锡特卡的温度
filename = 'D:/Sublime Text/python_test/16/sitka_weather_2018_simple.csv'
dates,highs,lows = [],[],[]
weather_data(filename,dates,highs,lows,t_date=2,t_high=5,t_low=6)

# 根据锡特卡的数据绘制图形
plt.style.use('seaborn')
fig,ax = plt.subplots()
ax.plot(dates,highs,c='red',alpha=
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值