Draw weather maps for individual regions
1. Sitka_Weather_Partial data in 2014.csv file

'''from csv obtain the highest and lowest temperatures in the sitka region for the corresponding dates in 2014 from the format file, and plot them accordingly'''
import csv
from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib import dates as mdate
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
filename='sitka_weather_2014.csv'
#open file
with open(filename) as f:
#read all lines of the file
reader=csv.reader(f)
#read a line from a file
header_row=next(reader)
#retrieve date, maximum temperature, and minimum temperature from the file
dates,highs,lows=[],[],[]
for row in reader:
#read the first column of each row of data
current_date=datetime.strptime(row[0],"%Y-%m-%d")
dates.append(current_date)
#read the second column of each row of data
high=int(row[1])
highs.append(high)
#read the 4th column of each row of data
low=int(row[3])
lows.append(low)
#draw charts based on data
fig=plt.figure(figsize=(10,6))
plt.plot(dates,highs, c='red',alpha=0.5)
plt.plot(dates,lows,c='blue',alpha=0.5)
#fill the drawing area with a color of blue
plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.1) #alpha set color transparency, default to 1, indicating complete opacity
plt.title("Daily high and low temperatures -2014", fontsize=24)
#set a new image at the original image position
ax=fig.add_subplot(1,1,1)
#set up x the format of the axis is [%b: abbreviations for months ][%Y: year ]
ax.xaxis.set_major_formatter(mdate.DateFormatter('%b %Y'))
#set up x axis range
plt.xticks(pd.date_range('2014-01','2014-12',freq='MS'),rotation=30) #freq='MS': set scale format to start of each month( month start frequency)
plt.xlabel(' ',fontsize=16)
fig.autofmt_xdate() #draw diagonal date labels to avoid overlapping with each other
plt.ylabel('Temperature (F)',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.show()
The diagram is as follows:
Reference for x-axis label setting:
https://www.jianshu.com/p/2a96f0cafbf1.
Add error checking code to resolve exceptions caused by missing data
2. Death_Valley_Partial data in 2014.csv file

import csv
from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib import dates as mdate
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
#retrieve date and maximum temperature from file
filename='death_valley_2014.csv'
#open file
with open(filename) as f:
#read all lines of the file
reader=csv.reader(f)
#read a line from a file
header_row=next(reader)
header_row=next(reader)
#retrieve date, maximum temperature, and minimum temperature from the file
dates,highs,lows=[],[],[]
for row in reader:
try:
#read the first column of each row of data
current_date=datetime.strptime(row[0],"%Y-%m-%d")
#read the second column of each row of data
high=int(row[1])
#read the 4th column of each row of data
low=int(row[3])
except ValueError:
#print an error message indicating missing data information
print(current_date,'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
#draw charts based on data
fig=plt.figure(figsize=(10,6))
plt.plot(dates,highs, c='red',alpha=0.5)
plt.plot(dates,lows,c='blue',alpha=0.5)
#fill the drawing area with a color of blue
plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.1) #alpha set color transparency, default to 1, indicating complete opacity
plt.title("Daily high and low temperatures -2014nDeath Valley, CA", fontsize=24)
#set a new image at the original image position
ax=fig.add_subplot(1,1,1)
#set up x the format of the axis is [%b: abbreviations for months ][%Y: year ]
ax.xaxis.set_major_formatter(mdate.DateFormatter('%b %Y'))
#set up x axis range
plt.xticks(pd.date_range('2014-01','2014-12',freq='MS'),rotation=30) #freq='MS': set scale format to start of each month( month start frequency)
plt.xlabel(' ',fontsize=16)
fig.autofmt_xdate() #draw diagonal date labels to avoid overlapping with each other
plt.ylabel('Temperature (F)',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.show()

Present two datasets in one chart
import csv
from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib import dates as mdate
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
def get_weather_data(filename,dates,highs,lows):
'''Get the highs and lows from a data file.'''
with open(filename) as f:
#read all lines of the file
reader=csv.reader(f)
#read a line from a file
header_row=next(reader)
for row in reader:
try:
#read the first column of each row of data
current_date=datetime.strptime(row[0],"%Y-%m-%d")
#read the second column of each row of data
high=int(row[1])
#read the 4th column of each row of data
low=int(row[3])
except ValueError:
print(current_date,'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
#draw a chart of the sitka region
dates,highs,lows=[],[],[]
get_weather_data('sitka_weather_2014.csv',dates,highs,lows)
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)
#fill the drawing area with a color of blue
plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.15) #alpha set color transparency, default to 1, indicating complete opacity
#draw a chart of the death valley region
dates,highs,lows=[],[],[]
get_weather_data('death_valley_2014.csv',dates,highs,lows)
plt.plot(dates,highs, c='red',alpha=0.3)
plt.plot(dates,lows,c='blue',alpha=0.3)
#fill the drawing area with a color of blue
plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.05) #alpha set color transparency, default to 1, indicating complete opacity
#format plot
title = "Daily high and low temperatures - 2014"
title += "n Sitka, AK and Death Valley, CA"
plt.title(title, fontsize=20)
#set a new image at the original image position
ax=fig.add_subplot(1,1,1)
#set up x the format of the axis is [%b: abbreviations for months ][%Y: year ]
ax.xaxis.set_major_formatter(mdate.DateFormatter('%b %Y'))
#set up x axis range
plt.xticks(pd.date_range('2014-01','2014-12',freq='MS'),rotation=30) #freq='MS': set scale format to start of each month( month start frequency)
plt.xlabel(' ',fontsize=16)
fig.autofmt_xdate() #draw diagonal date labels to avoid overlapping with each other
plt.ylabel('Temperature (F)',fontsize=16)
#plt.ylim(ymin=0,ymax=120)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.show()
The diagram is as follows:

Rainfall mapping
Data source: https://github.com/ehmatthes/pcc/blob/gh-pages/resources/sitka_rainfall_2015.csv.
import csv
from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib import dates as mdate
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
# Get dates and rainfall data from data file
# raindata is in column 19
filename = 'sitka_rainfall_2015.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
dates, rainfalls=[],[]
for row in reader:
try:
current_date=datetime.strptime(row[0],"%Y/%m/%d")
rainfall=float(row[19])
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, AK"
plt.title(title,fontsize=20)
ax=fig.add_subplot(1,1,1)
ax.xaxis.set_major_formatter(mdate.DateFormatter('%b %Y'))
plt.xticks(pd.date_range('2015-01','2015-12',freq='MS'),rotation=30) #freq='MS': set scale format to start of each month( month start frequency)
plt.xlabel('',fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Rainfall (in)", fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.show()
