已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Solutions - Chapter 1616-2: Sitka-Death Valley ComparisonThe temperature scales on the Sitka and Death Valley graphs reflect the different ranges of the data. To accurately compare the temperature range in Sitka to that of Death Valley, you need identical scales on the y-axis. Change the settings for the y-axis on one or both of the charts in Figures 16-5 and 16-6, and make a direct comparison between temperature ranges in Sitka and Death Valley (or any two places you want to compare). You can also try plotting the two data sets on the same chart.Thepyplotfunctionylim()allows you to set the limits of just the y-axis. If you ever need to specify the limits of the x-axis, theres a correspondingxlim()function as well.import csvfrom datetime import datetimefrom matplotlib import pyplot as plt# Get dates, high, and low temperatures from file.filename = sitka_weather_2014.csvwith open(filename) as f: reader = csv.reader(f) header_row = next(reader) dates, highs, lows = , , for row in reader: try: current_date = datetime.strptime(row0, %Y-%m-%d) high = int(row1) low = int(row3) except ValueError: print(current_date, missing data) else: dates.append(current_date) highs.append(high) lows.append(low)# Plot data.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)# Format plot.title = Daily high and low temperatures - 2014nSitka, AKplt.title(title, fontsize=20)plt.xlabel(, fontsize=16)fig.autofmt_xdate()plt.ylabel(Temperature (F), fontsize=16)plt.tick_params(axis=both, which=major, labelsize=16)plt.ylim(10, 120)plt.show()Output:Using the same limits for theylim()function with the Death Valley data results in a chart that has the same scale:There are a number of ways you can approach plotting both data sets on the same chart. In the following solution, we put the code for reading the csv file into a function. We then call it once to grab the highs and lows for Sitka before making the chart, and then call the function a second time to add Death Valleys data to the existing plot. The colors have been adjusted slightly to make each locations data distinct.import csvfrom datetime import datetimefrom matplotlib import pyplot as pltdef get_weather_data(filename, dates, highs, lows): Get the highs and lows from a data file. with open(filename) as f: reader = csv.reader(f) header_row = next(reader) # dates, highs, lows = , , for row in reader: try: current_date = datetime.strptime(row0, %Y-%m-%d) high = int(row1) low = int(row3) except ValueError: print(current_date, missing data) else: dates.append(current_date) highs.append(high) lows.append(low)# Get weather data for Sitka.dates, highs, lows = , , get_weather_data(sitka_weather_2014.csv, dates, highs, lows)# Plot Sitka weather data.fig = plt.figure(dpi=128, figsize=(10, 6)plt.plot(dates, highs, c=red, alpha=0.6)plt.plot(dates, lows, c=blue, alpha=0.6)plt.fill_between(dates, highs, lows, facecolor=blue, alpha=0.15)# Get Death Valley data.dates, highs, lows = , , get_weather_data(death_valley_2014.csv, dates, highs, lows)# Add Death Valley data to current plot.plt.plot(dates, highs, c=red, alpha=0.3)plt.plot(dates, lows, c=blue, alpha=0.3)plt.fill_between(dates, highs, lows, facecolor=blue, alpha=0.05)# Format plot.title = Daily high and low temperatures - 2014title += nSitka, AK and Death Valley, CAplt.title(title, fontsize=20)plt.xlabel(, fontsize=16)fig.autofmt_xdate()plt.ylabel(Temperature (F), fontsize=16)plt.tick_params(axis=both, which=major, labelsize=16)plt.ylim(10, 120)plt.show()16-3: RainfallChoose any location youre interested in, and make a visualization that plots its rainfall. Start by focusing on one months data, and then once your code is working, run it for a full years data.Note:You can find the data file for this examplehere.import csvfrom datetime import datetimefrom matplotlib import pyplot as plt# Get dates and rainfall data from data file.# Rainfall data is in column 19.filename = sitka_rainfall_2015.csvwith open(filename) as f: reader = csv.reader(f) header_row = next(reader) dates, rainfalls = , for row in reader: try: current_date = datetime.strptime(row0, %Y-%m-%d) rainfall = float(row19) except ValueError: print(current_date, missing data) else: dates.append(current_date) rainfalls.append(rainfall)# Plot data.fig = plt.figure(dpi=128, figsize=(10, 6)plt.plot(dates, rainfalls, c=blue, alpha=0.5)plt.fill_between(dates, rainfalls, facecolor=blue, alpha=0.2)# Format plot.title = Daily rainfall amounts - 2015nSitka, AKplt.title(title, fontsize=20)plt.xlabel(, fontsize=16)fig.autofmt_xdate()plt.ylabel(Rainfall (in), fontsize=16)plt.tick_params(axis=both, which=major, labelsize=16)plt.show()Output:16-4: ExploreGenerate a few more visualizations that examine any other weather aspect youre interested in for any locations youre curious about.I live in a rainforest, so I was interested in playing with the rainfall data. I calculated the cumulative rainfall for the year, and plotted that over the daily rainfall. Even after living in this rain, Im surprised to see how much we get.import csvfrom datetime import datetimefrom matplotlib import pyplot as plt# Get dates and rainfall data from data file.# Rainfall data is in column 19.filename = sitka_rainfall_2015.csvwith open(filename) as f: reader = csv.reader(f) header_row = next(reader) dates, rainfalls, totals = , , for row in reader: try: current_date = datetime.strptime(row0, %Y-%m-%d) rainfall = float(row19) except ValueError: print(current_date, missing data) else:dates.append(current_date) rain
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年下半年吉林通化市辉南县事业单位专项招聘高层次和急需紧缺工作人员23人(6号)易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年吉林省长春市公用事业信息中心招聘4人易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年吉林市事业单位招聘工作人员(含基层治理专干)2093人(2号)易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年吉安市城乡规划建设局招考庐陵文化生态公园管理处讲解员易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年合肥市肥西县城市管理局市场监督管理局所属事业单位招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年台州椒江区环境保护分局招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年厦门海沧区城市管理行政执法局招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年厦门大学嘉庚学院建筑学系专任教师招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年南宁宾阳县招商中心招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年南京化工公司校园招聘【50人】易考易错模拟试题(共500题)试卷后附参考答案
- 神经外科临床诊疗指南及操作规范
- DB42-T 1989-2023 城乡公益性安葬设施建设与管理规范
- 2025国家开放大学《小学语文教学研究》形考任务1-5答案
- 肠内营养支持护理指南
- 教师名师笔试题库及答案
- 铁路营业线施工安全管理实施细则
- 2025年共同策划城市更新改造项目策划协议
- 生物样本库标准操作流程
- 人教版初一到初三英语单词
- 循证思维在临床护理教学中的应用
- 围手术期管理制度与流程
评论
0/150
提交评论