Plotting Graphs + Bestfit lines

A

arslanburney

Hello. Ive got two functions here. Somehow the program does not go in
to the second function wehn i call it. The bestfit function. Could
some1 help me identify the problem. Heres the code:


import Gnuplot

def bestfit(uinput):

if not isinstance(uinput, list):
return False

else:


sigmax = sigmay = sigmaxy = sigmaxwhl = sigmaxsq = 0

for i in range(len(uinput)):

n = len(uinput)

sigmax = uinput[0] + sigmax
sigmay = uinput[1] + sigmay
sigmaxy = uinput[0] * uinput [1] + sigmaxy
sigmaxwhl = sigmax * sigmax
sigmaxsq = uinput[0] * uinput[0] + sigmaxsq
sigmaxsigmay = sigmax * sigmay

num = sigmaxsigmay - (n * sigmaxy)
den = sigmaxwhl - (n* sigmaxsq)

num2 = (sigmax * sigmaxy) - (sigmay * sigmaxsq)


gradient = num / den

intercept = num2 / den

m = gradient
c = intercept

p = Gnuplot.Gnuplot()
p.plot ('%f * x+%f'%(m,c))

return p

def plot(original, expected, actual):


if not isinstance(original, list):
return False

else:

gp = Gnuplot.Gnuplot()
gp('set data style lines')



# Make the plot items
plot1 = Gnuplot.PlotItems.Data(original, title="Original")
plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
plot3 = Gnuplot.PlotItems.Data(actual, title="Acutal")


gp.plot(plot1, plot2, plot3)
bestfit(expected)
bestfit(actual)

return gp


-------

import Combine #The name of my file...

gp = Combine.plot( [(2,3), (4,8), (5,9), (6,2)], [(1,7), (3,3), (4,5),
(5,6)], [(1,3), (3,10), (4,8), (7,9) ] )
raw_input()
 
P

Peter Otten

Hello. Ive got two functions here. Somehow the program does not go in
to the second function wehn i call it. The bestfit function. Could
some1 help me identify the problem. Heres the code:

Same problem as before, you have to keep the Gnuplot instance alive if you
want to see the graph.

Note that instead of

for i in range(len(uinput)):
sigmaxy = uinput[0] * uinput[1] + sigmaxy

you could write

for x, y in uinput:
sigmaxy += x * y

High time to take a look into a good Python tutorial...

# --- combine.py ---
import Gnuplot

def bestfit(uinput):
sigmax = sigmay = sigmaxy = sigmaxwhl = sigmaxsq = 0

for i in range(len(uinput)):

n = len(uinput)

sigmax = uinput[0] + sigmax
sigmay = uinput[1] + sigmay
sigmaxy = uinput[0] * uinput [1] + sigmaxy
sigmaxwhl = sigmax * sigmax
sigmaxsq = uinput[0] * uinput[0] + sigmaxsq
sigmaxsigmay = sigmax * sigmay

num = sigmaxsigmay - (n * sigmaxy)
den = sigmaxwhl - (n* sigmaxsq)

num2 = (sigmax * sigmaxy) - (sigmay * sigmaxsq)


gradient = num / den

intercept = num2 / den

m = gradient
c = intercept

p = Gnuplot.Gnuplot()
p.plot ('%f * x+%f'%(m,c))

return p

def plot(original, expected, actual):
gp = Gnuplot.Gnuplot()
gp('set data style lines')



# Make the plot items
plot1 = Gnuplot.PlotItems.Data(original, title="Original")
plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
plot3 = Gnuplot.PlotItems.Data(actual, title="Acutal")


gp.plot(plot1, plot2, plot3)
return gp


def show_plots(original, expected, actual):
gp = combine.plot( original, expected, actual)
raw_input("first")
gp = combine.bestfit(expected)
raw_input("second")
gp = combine.bestfit(actual)
raw_input("third")

# --- combine_main.py ---
import combine

combine.show_plots([(2,3), (4,8), (5,9), (6,2)], [(1,7), (3,3), (4,5),
(5,6)], [(1,3), (3,10), (4,8), (7,9) ] )
 
A

arslanburney

Umm.... Tried this out too.... Laiken heres the error that this
gives..

Traceback (most recent call last):
File "D:\Questions\Gradient and C\Gnuplot\Combining Best fit and
Plotting\combasd.py", line 3, in <module>
combine.show_plots([(2,3), (4,8), (5,9), (6,2)], [(1,7), (3,3),
(4,5), (5,6)], [(1,3), (3,10), (4,8), (7,9) ] )
File "D:\Questions\Gradient and C\Gnuplot\Combining Best fit and
Plotting\combine.py", line 54, in show_plots
gp = combine.plot( original, expected, actual)
NameError: global name 'combine' is not defined

Still confused though i get the instance part ur trying to tell me.
 
P

Peter Otten

Umm.... Tried this out too.... Laiken heres the error that this
gives..

Traceback (most recent call last):
File "D:\Questions\Gradient and C\Gnuplot\Combining Best fit and
Plotting\combasd.py", line 3, in <module>
combine.show_plots([(2,3), (4,8), (5,9), (6,2)], [(1,7), (3,3),
(4,5), (5,6)], [(1,3), (3,10), (4,8), (7,9) ] )
File "D:\Questions\Gradient and C\Gnuplot\Combining Best fit and
Plotting\combine.py", line 54, in show_plots
gp = combine.plot( original, expected, actual)
NameError: global name 'combine' is not defined

Still confused though i get the instance part ur trying to tell me.

Sorry, it should have been

def show_plots(original, expected, actual):
gp = plot( original, expected, actual)
raw_input("first")
gp = bestfit(expected)
raw_input("second")
gp = bestfit(actual)
raw_input("third")


Peter
 
A

arslanburney

Umm.... Tried this out too.... Laiken heres the error that this
gives..
Traceback (most recent call last):
  File "D:\Questions\Gradient and C\Gnuplot\Combining Best fit and
Plotting\combasd.py", line 3, in <module>
    combine.show_plots([(2,3), (4,8), (5,9), (6,2)], [(1,7), (3,3),
(4,5), (5,6)], [(1,3), (3,10), (4,8), (7,9) ] )
  File "D:\Questions\Gradient and C\Gnuplot\Combining Best fit and
Plotting\combine.py", line 54, in show_plots
    gp = combine.plot( original, expected, actual)
NameError: global name 'combine' is not defined
Still confused though i get the instance part ur trying to tell me.

Sorry, it should have been

def show_plots(original, expected, actual):
    gp = plot( original, expected, actual)
    raw_input("first")
    gp = bestfit(expected)
    raw_input("second")
    gp = bestfit(actual)
    raw_input("third")

Peter

Tried that out too. No error however, best fit lines still not being
made on the graph. Only the 3 plot lines show up.
 
P

Peter Otten

Tried that out too. No error however, best fit lines still not being
made on the graph. Only the 3 plot lines show up.

Sorry, I don't know gnuplot, so I can't help you with any but the obvious
(read: Python) errors.

Peter
 
P

Peter Otten

Tried that out too. No error however, best fit lines still not being
made on the graph. Only the 3 plot lines show up.

Gave it another shot. You might want something like

from __future__ import division
import Gnuplot

def bestfit(uinput, **kw):
sigmax = sigmay = sigmaxy = sigmaxsq = 0

for x, y in uinput:
sigmax += x
sigmay += y
sigmaxy += x * y
sigmaxsq += x * x

n = len(uinput)
sigmaxwhl = sigmax * sigmax
sigmaxsigmay = sigmax * sigmay
num = sigmaxsigmay - n * sigmaxy
den = sigmaxwhl - n * sigmaxsq
num2 = sigmax * sigmaxy - sigmay * sigmaxsq


gradient = num / den
intercept = num2 / den

return Gnuplot.Func('%f * x+%f' % (gradient, intercept), **kw)

def plot(original, expected, actual):
gp = Gnuplot.Gnuplot()
gp('set data style lines')

# Make the plot items
plot1 = Gnuplot.PlotItems.Data(original, title="Original")
plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
plot3 = Gnuplot.PlotItems.Data(actual, title="Actual")
bf2 = bestfit(expected, title="Best fit expected")
bf3 = bestfit(actual, title="Best fit actual")

gp.plot(plot1, plot2, plot3, bf2, bf3)
return gp


if __name__ == "__main__":
gp = plot( [(2,3), (4,8), (5,9), (6,2)], [(1,7), (3,3), (4,5), (5,6)],
[(1,3), (3,10), (4,8), (7,9) ] )
raw_input()

It's all in one file for simplicity. Note that I did not check the best fit
algorithm, just tried to simplify what you already had. Use at your own
risk.

Peter
 
A

arslanburney

Got the problem solved finally. Missed out theses two lines:

plot1 = Gnuplot.PlotItems.Data(original, title="Original")
plot2 = Gnuplot.PlotItems.Data(expected, title="Expected")
plot3 = Gnuplot.PlotItems.Data(actual, title="Acutal")
plot4 = Gnuplot.PlotItems.Func('%f * x+%f'%(bf1[0],bf1[1]), title
= "Expected Best Fit")
plot5 = Gnuplot.PlotItems.Func('%f * x+%f'%(bf2[0],bf2[1]), title
= "Actual Best Fit")

The last 2 ones.... thnx nyways
 
B

Bas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top