Plotting Graphs + Bestfit lines

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arslanburney@gmail.com

    Plotting Graphs + Bestfit lines

    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(uinp ut, list):
    return False

    else:


    sigmax = sigmay = sigmaxy = sigmaxwhl = sigmaxsq = 0

    for i in range(len(uinpu t)):

    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(orig inal, list):
    return False

    else:

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



    # Make the plot items
    plot1 = Gnuplot.PlotIte ms.Data(origina l, title="Original ")
    plot2 = Gnuplot.PlotIte ms.Data(expecte d, title="Expected ")
    plot3 = Gnuplot.PlotIte ms.Data(actual, title="Acutal")


    gp.plot(plot1, plot2, plot3)
    bestfit(expecte d)
    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()

  • Peter Otten

    #2
    Re: Plotting Graphs + Bestfit lines

    arslanburney@gm ail.com 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(uinpu t)):
    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(uinpu t)):

    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.PlotIte ms.Data(origina l, title="Original ")
    plot2 = Gnuplot.PlotIte ms.Data(expecte d, title="Expected ")
    plot3 = Gnuplot.PlotIte ms.Data(actual, title="Acutal")


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


    def show_plots(orig inal, expected, actual):
    gp = combine.plot( original, expected, actual)
    raw_input("firs t")
    gp = combine.bestfit (expected)
    raw_input("seco nd")
    gp = combine.bestfit (actual)
    raw_input("thir d")

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

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


    Comment

    • arslanburney@gmail.com

      #3
      Re: Plotting Graphs + Bestfit lines

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

      Traceback (most recent call last):
      File "D:\Questions\G radient and C\Gnuplot\Combi ning Best fit and
      Plotting\combas d.py", line 3, in <module>
      combine.show_pl ots([(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\G radient and C\Gnuplot\Combi ning Best fit and
      Plotting\combin e.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.

      Comment

      • Peter Otten

        #4
        Re: Plotting Graphs + Bestfit lines

        arslanburney@gm ail.com wrote:
        Umm.... Tried this out too.... Laiken heres the error that this
        gives..
        >
        Traceback (most recent call last):
        File "D:\Questions\G radient and C\Gnuplot\Combi ning Best fit and
        Plotting\combas d.py", line 3, in <module>
        combine.show_pl ots([(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\G radient and C\Gnuplot\Combi ning Best fit and
        Plotting\combin e.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(orig inal, expected, actual):
        gp = plot( original, expected, actual)
        raw_input("firs t")
        gp = bestfit(expecte d)
        raw_input("seco nd")
        gp = bestfit(actual)
        raw_input("thir d")


        Peter

        Comment

        • arslanburney@gmail.com

          #5
          Re: Plotting Graphs + Bestfit lines

          On Jun 13, 12:13 pm, Peter Otten <__pete...@web. dewrote:
          arslanbur...@gm ail.com wrote:
          Umm.... Tried this out too.... Laiken heres the error that this
          gives..
          >
          Traceback (most recent call last):
            File "D:\Questions\G radient and C\Gnuplot\Combi ning Best fit and
          Plotting\combas d.py", line 3, in <module>
              combine.show_pl ots([(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\G radient and C\Gnuplot\Combi ning Best fit and
          Plotting\combin e.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(orig inal, expected, actual):
              gp = plot( original, expected, actual)
              raw_input("firs t")
              gp = bestfit(expecte d)
              raw_input("seco nd")
              gp = bestfit(actual)
              raw_input("thir d")
          >
          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.

          Comment

          • Peter Otten

            #6
            Re: Plotting Graphs + Bestfit lines

            arslanburney@gm ail.com 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

            Comment

            • Peter Otten

              #7
              Re: Plotting Graphs + Bestfit lines

              arslanburney@gm ail.com 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.PlotIte ms.Data(origina l, title="Original ")
              plot2 = Gnuplot.PlotIte ms.Data(expecte d, title="Expected ")
              plot3 = Gnuplot.PlotIte ms.Data(actual, title="Actual")
              bf2 = bestfit(expecte d, 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

              Comment

              • arslanburney@gmail.com

                #8
                Re: Plotting Graphs + Bestfit lines

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

                plot1 = Gnuplot.PlotIte ms.Data(origina l, title="Original ")
                plot2 = Gnuplot.PlotIte ms.Data(expecte d, title="Expected ")
                plot3 = Gnuplot.PlotIte ms.Data(actual, title="Acutal")
                plot4 = Gnuplot.PlotIte ms.Func('%f * x+%f'%(bf1[0],bf1[1]), title
                = "Expected Best Fit")
                plot5 = Gnuplot.PlotIte ms.Func('%f * x+%f'%(bf2[0],bf2[1]), title
                = "Actual Best Fit")

                The last 2 ones.... thnx nyways

                Comment

                Working...