Software Carpentry Demo - June 7, 2012

Note: IPython is running here in --pylab mode so I didn't have to import matplotlib.

In [1]:
import numpy as np
import scipy.stats as spst
import scipy.optimize as spop
import scipy.interpolate as spint
In [2]:
year, aus, can = np.loadtxt('temperatures.txt', delimiter=',', 
                            skiprows=1, unpack=True)
In [3]:
plot(year, aus)
title('Australia')
xlabel('Year')
ylabel('Temperature (C)')
Out[3]:
<matplotlib.text.Text at 0x105ba3ed0>
In [4]:
plot(year, can)
title('Canada')
xlabel('Year')
ylabel('Temperature (C)')
Out[4]:
<matplotlib.text.Text at 0x105c88c90>

Fit a line to the Australia temperatures using scipy.stats.linregress: http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html

In [5]:
aus_line = spst.linregress(year, aus)
In [6]:
aus_line_pts = aus_line[0] * year + aus_line[1]
In [7]:
plot(year, aus)
plot(year, aus_line_pts)
title('Australia')
xlabel('Year')
ylabel('Temperature (C)')
Out[7]:
<matplotlib.text.Text at 0x105e82e90>

Fit a line to the Canada data using scipy.optimize.curve_fit: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

In [8]:
def linear_func(year, slope, intercept):
    return slope * year + intercept
In [9]:
can_line, can_cov = spop.curve_fit(linear_func, year, can)
In [10]:
can_line_pts = linear_func(year, can_line[0], can_line[1])
In [11]:
plot(year, can)
plot(year, can_line_pts)
title('Canada')
xlabel('Year')
ylabel('Temperature (C)')
Out[11]:
<matplotlib.text.Text at 0x105ec0150>
In [12]:
spst.pearsonr(year, can)
Out[12]:
(0.51195782671512846, 1.2713276823278399e-08)

Fit a spline to the Australia data using scipy.interpolate.UnivariateSpline: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.UnivariateSpline.html

In [13]:
spline = spint.UnivariateSpline(year, aus, k=5)
In [14]:
x = np.linspace(year.min(), year.max(), 1000)
In [15]:
y = spline(x)
In [16]:
plot(year, aus)
plot(x, y)
title('Australia')
xlabel('Year')
ylabel('Temperature (C)')
Out[16]:
<matplotlib.text.Text at 0x10620e6d0>

Plot the temperatures against each other and fit a line.

In [17]:
plot(aus, can, 'bo')
xlabel('Australia Temp (C)')
ylabel('Canada Temp (C)')
Out[17]:
<matplotlib.text.Text at 0x1062b47d0>
In [18]:
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)')
Out[18]:
<matplotlib.text.Text at 0x1062ea450>
In [19]:
new_line
Out[19]:
(0.8255661193335796,
 -25.090352153252795,
 0.44948681311947275,
 9.4409823433233215e-07,
 0.15861125998986189)

Plot the temperatures as variations about their respective best fit lines.

In [20]:
aus_sub = aus - aus_line_pts
In [21]:
can_sub = can - can_line_pts
In [22]:
plot(year, aus_sub, label='Aus')
plot(year, can_sub, label='Can')
xlabel('Year')
ylabel('Temperature Variation (C)')
legend(loc='lower left')
Out[22]:
<matplotlib.legend.Legend at 0x106593f10>
In [22]: