\"\"\" This problem asks you to read yearly temperature deviation data from the
ID: 3601059 • Letter: #
Question
"""
This problem asks you to read yearly temperature deviation data from the GLB.Ts+dSST.csv file,
transform the data from str to float, then plot them.
You are required to do linear regression on the January and August data and plot them as well.
"""
import numpy as np
import scipy.linalg as linalg
import matplotlib.pyplot as plt
import csv #needed to read csv file
import least_square as LS #will use the least square functions there for linear regression
#----------------------------------------------
def plot_GLOTI(datafile="GLB.Ts+dSST.csv"):
#
# read from the datafile, skip any comment lines that start with #
#
with open(datafile) as csvf:
LOTIdata = csv.DictReader(filter(lambda row: row[0]!='#', csvf))
#
# you are asked to get the data for the following 4 months
#
Jan=[]; Apr=[]; Jul=[]; Aug=[]
# you also need the Year for the xlabel
Year=[]
#
#study the online manual for the csv.DictReader() function, then finish the following part
#to save appropriate data in the above empty lists, remember you readin only str, but you
#need float datatype for the plots
#
for line in LOTIdata:
#
# plot the temperature deviation data of the four months you obtained from the csv file
#
#
## do linear regression on the Jan and Aug data you obtained, using the
## least_square_poly() function from the imported LS module
## (two sample lines are provided below, you need to modify accordingly)
#
xv = np.arange(Year[0], Year[-1]+0.5, 0.5) #extrapolate over the Years
#JulLS, Julstd = LS.least_square_poly(Year, Jul, 1, xv) #change this line to fit the Jan and Aug data
#
# plot the linear regressions for the Jan and Aug data
#
#
# set legends, title, labels properly
#
plt.savefig("plot_GLOTI.png")
plt.show()
Explanation / Answer
import numpy as np
import scipy.linalg as linalg
import matplotlib.pyplot as plt
import csv #needed to read csv file
import least_square as LS #will use the least square functions there for linear regression
#----------------------------------------------
def plot_GLOTI(datafile="GLB.Ts+dSST.csv"):
#
# read from the datafile, skip any comment lines that start with #
#
with open(datafile) as csvf:
LOTIdata = csv.DictReader(filter(lambda row: row[0]!='#', csvf))
#
# you are asked to get the data for the following 4 months
#
Jan=[]; Apr=[]; Jul=[]; Aug=[]
# you also need the Year for the xlabel
Year=[]
#
#study the online manual for the csv.DictReader() function, then finish the following part
#to save appropriate data in the above empty lists, remember you readin only str, but you
#need float datatype for the plots
#
for line in LOTIdata:
#
# plot the temperature deviation data of the four months you obtained from the csv file
#
#
## do linear regression on the Jan and Aug data you obtained, using the
## least_square_poly() function from the imported LS module
## (two sample lines are provided below, you need to modify accordingly)
#
xv = np.arange(Year[0], Year[-1]+0.5, 0.5) #extrapolate over the Years
#JulLS, Julstd = LS.least_square_poly(Year, Jul, 1, xv) #change this line to fit the Jan and Aug data
#
# plot the linear regressions for the Jan and Aug data
#
#
# set legends, title, labels properly
#
plt.savefig("plot_GLOTI.png")
plt.show()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.