Plotting Graphs + Bestfit lines

Discussion in 'Python' started by arslanburney@gmail.com, Jun 13, 2008.

  1. Guest

    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()
     
    , Jun 13, 2008
    #1
    1. Advertising

  2. Peter Otten Guest

    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[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) ] )
     
    Peter Otten, Jun 13, 2008
    #2
    1. Advertising

  3. Guest

    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.
     
    , Jun 13, 2008
    #3
  4. Peter Otten Guest

    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,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
     
    Peter Otten, Jun 13, 2008
    #4
  5. Guest

    On Jun 13, 12:13 pm, Peter Otten <> wrote:
    > 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,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.
     
    , Jun 13, 2008
    #5
  6. Peter Otten Guest

    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
     
    Peter Otten, Jun 13, 2008
    #6
  7. Peter Otten Guest

    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,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
     
    Peter Otten, Jun 13, 2008
    #7
  8. Guest

    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
     
    , Jun 13, 2008
    #8
  9. Bas Guest

    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
     
    Bas, Jun 13, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morc
    Replies:
    2
    Views:
    693
    IchBin
    Feb 23, 2006
  2. Vivek

    plotting Graphs how to in C ?

    Vivek, Oct 16, 2006, in forum: C Programming
    Replies:
    2
    Views:
    922
  3. Replies:
    3
    Views:
    352
    Helmut Jarausch
    Jun 13, 2008
  4. nk1

    Plotting graphs in python

    nk1, Oct 12, 2010, in forum: Python
    Replies:
    0
    Views:
    303
  5. Vicky Conlan

    Advice on module for plotting graphs

    Vicky Conlan, Sep 30, 2008, in forum: Perl Misc
    Replies:
    10
    Views:
    180
    A. Sinan Unur
    Oct 2, 2008
Loading...

Share This Page