




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 子罕辞玉的课件
- 年月日说课课件
- 年度安全培训调查汇报
- 年底安全培训要点总结课件
- 威驰及竞品课件
- 年后复工安全培训
- 玉溪市统一遴选和选调公务员笔试真题2024
- 威尼斯的小艇课件近义词
- 常用版固定期限聘用合同书5篇
- 平面设计配色培训课件
- 前端Vue3项目实战教程
- 2025年建筑水电安装工程师执业资格考试试题及答案解析
- 智算中心高性能计算系统设计方案
- 散装柴油管理制度
- 12S522混凝土模块式排水检查井图集
- 2025年茅台酒厂考试试题及答案
- (20250731)房屋市政工程基孔肯雅热、登革热防控检查(自查)表
- 生物性资产管理办法
- 体重控制健康宣教
- 2025年浙江省人事考试工作(4月26日事业单位笔试)笔试历年典型考题及考点剖析附带答案详解
- 机械加工工艺与工具知识测试试卷
评论
0/150
提交评论