Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Plotting Graphs + Bestfit lines

Reply
Thread Tools

Plotting Graphs + Bestfit lines

 
 
arslanburney@gmail.com
Guest
Posts: n/a
 
      06-13-2008
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[i][0] + sigmax
sigmay = uinput[i][1] + sigmay
sigmaxy = uinput[i][0] * uinput [i][1] + sigmaxy
sigmaxwhl = sigmax * sigmax
sigmaxsq = uinput[i][0] * uinput[i][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,, (5,9), (6,2)], [(1,7), (3,3), (4,5),
(5,6)], [(1,3), (3,10), (4,, (7,9) ] )
raw_input()

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      06-13-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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[i][0] * uinput[i][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[i][0] + sigmax
sigmay = uinput[i][1] + sigmay
sigmaxy = uinput[i][0] * uinput [i][1] + sigmaxy
sigmaxwhl = sigmax * sigmax
sigmaxsq = uinput[i][0] * uinput[i][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,, (5,9), (6,2)], [(1,7), (3,3), (4,5),
(5,6)], [(1,3), (3,10), (4,, (7,9) ] )


 
Reply With Quote
 
 
 
 
arslanburney@gmail.com
Guest
Posts: n/a
 
      06-13-2008
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,, (5,9), (6,2)], [(1,7), (3,3),
(4,5), (5,6)], [(1,3), (3,10), (4,, (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.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-13-2008
(E-Mail Removed) wrote:

> 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,, (5,9), (6,2)], [(1,7), (3,3),
> (4,5), (5,6)], [(1,3), (3,10), (4,, (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
 
Reply With Quote
 
arslanburney@gmail.com
Guest
Posts: n/a
 
      06-13-2008
On Jun 13, 12:13*pm, Peter Otten <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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,, (5,9), (6,2)], [(1,7), (3,3),
> > (4,5), (5,6)], [(1,3), (3,10), (4,, (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.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-13-2008
(E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-13-2008
(E-Mail Removed) wrote:

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


> 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,, (5,9), (6,2)], [(1,7), (3,3), (4,5), (5,6)],
[(1,3), (3,10), (4,, (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
 
Reply With Quote
 
arslanburney@gmail.com
Guest
Posts: n/a
 
      06-13-2008
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
 
Reply With Quote
 
Bas
Guest
Posts: n/a
 
      06-13-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_inter sect,y_intersect,'o')
show()

HTH,
Bas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plotting graphs in python nk1 Python 0 10-12-2010 08:40 AM
Plotting Graphs using Gnuplot arslanburney@gmail.com Python 3 06-13-2008 07:51 AM
plotting Graphs how to in C ? Vivek C Programming 2 10-16-2006 09:22 PM
Creating And Plotting Graphs morc Java 2 02-23-2006 08:22 PM
Plotting simple line graphs in Visual Basic 5 Richard Owlett Computer Support 0 02-16-2004 05:19 PM



Advertisments