import numpy as np import scipy.stats as spst import scipy.optimize as spop import scipy.interpolate as spint year, aus, can = np.loadtxt('temperatures.txt', delimiter=',', skiprows=1, unpack=True) plot(year, aus) title('Australia') xlabel('Year') ylabel('Temperature (C)') plot(year, can) title('Canada') xlabel('Year') ylabel('Temperature (C)') aus_line = spst.linregress(year, aus) aus_line_pts = aus_line[0] * year + aus_line[1] plot(year, aus) plot(year, aus_line_pts) title('Australia') xlabel('Year') ylabel('Temperature (C)') def linear_func(year, slope, intercept): return slope * year + intercept can_line, can_cov = spop.curve_fit(linear_func, year, can) can_line_pts = linear_func(year, can_line[0], can_line[1]) plot(year, can) plot(year, can_line_pts) title('Canada') xlabel('Year') ylabel('Temperature (C)') spst.pearsonr(year, can) spline = spint.UnivariateSpline(year, aus, k=5) x = np.linspace(year.min(), year.max(), 1000) y = spline(x) plot(year, aus) plot(x, y) title('Australia') xlabel('Year') ylabel('Temperature (C)') plot(aus, can, 'bo') xlabel('Australia Temp (C)') ylabel('Canada Temp (C)') new_line = spst.linregress(aus, can) new_line_pts = new_line[0] * aus + new_line[1] plot(aus, can, 'bo') plot(aus, new_line_pts, 'r-') xlabel('Australia Temp (C)') ylabel('Canada Temp (C)') new_line aus_sub = aus - aus_line_pts can_sub = can - can_line_pts plot(year, aus_sub, label='Aus') plot(year, can_sub, label='Can') xlabel('Year') ylabel('Temperature Variation (C)') legend(loc='lower left')