Plotting Graphs + Bestfit lines

Bas wegwerp at gmail.com
Fri Jun 13 08:01:16 EDT 2008


I am not going to reverse engineer your code, but it looks like your
writing your own least-squares fitting algorithm and doing some simple
plots. Also, from your many posts last days, it looks like you are a
newbie struggling with a python interface to gnuplot.

May I suggest that you have a look at numpy/scipy/matplotlib instead?
With those, the things that you are trying to do could be done
trivially in a just a few lines. What you want to do could be done
with something like (untested!):

from pylab import *
xdata = ... %whatever you get it from
ydata = ...

p = polyfit(xdata, ydata, 1) %fit 1st order polynomial
plot(xdata, ydata, xdata, 'o', polyval(p, xdata)) %plot original data
with fit
show()

Things like fitting algorithms are already included, so you can focus
your energy on the real work. E.g., if you later want to change to a
quadratic fit instead of a linear, you just change 1 to 2 in the
polyfit. As you said in one of your other posts, you need to find the
intersection point of two lines. If poly1 and poly2 are the
polynomials describing the lines, you would get your answer as

x_intersect = roots(poly1 - poly2)
y_intersect = polyval(poly1,x_intersect)
plot(x,polyval(poly1,x),x,polyval(poly2,x),x_intersect,y_intersect,'o')
show()

HTH,
Bas



More information about the Python-list mailing list