linear.py
#!/usr/bin/env python
'''
linear.py
Eric Ayars
10/4/16
Graphing demonstration showing how to create simple
plots and linear fits relevant to Advanced Lab.
'''
from pylab import *
# Data (Made-up, with made-up error)
x = array([1, 2, 3, 4, 5])
y = array([4.7, 6.6, 7.8, 11.5, 14.7])
sigma = array([2.1, 1.4, 1.0, 0.4, 0.2])
xx = linspace(0,5,100)
# Linear plot, with curve fits
errorbar(x, y, yerr=sigma, fmt='bo')
title('Position Data')
xlabel('Time (Fortnights)')
ylabel('Distance (Furlongs)')
# First-order least-squares fit
# unweighted fit
params, cov = polyfit(x,y,1,cov=True)
B = params[0]
A = params[1]
dB = sqrt(cov[0,0])
dA = sqrt(cov[1,1])
plot(xx, A + B*xx, 'r-')
# weighted fit
params, cov = polyfit(x,y,1,w=1.0/sigma**2,cov=True)
B = params[0]
A = params[1]
dB = sqrt(cov[0,0])
dA = sqrt(cov[1,1])
plot(xx, A + B*xx, 'g-')
# adjust x limits if necessary
xlim(0,6)
# If you want to save the graph...
#savefig('linear_plot.pdf')
show()
Generated by GNU Enscript 1.6.6.