pyplot and best fit lines/polynomials?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

pyplot and best fit lines/polynomials?

Mike Rooney
Hi everyone,

I have been using pyplot a little and it sure is easy and quite fast!
Recently I wanted to have a best-fit curve to my data and I couldn't
find a built-in way to do this, so I added a little class to plot.py:

class PolyBestFitLine(PolyLine):
    """
    Acts just like a PolyLine except that the Line
    will be the best-fit polynomial of the points
    given, with degree N.
    """
    def __init__(self, points, N=1, **attr):
        xs = tuple((p[0] for p in points))
        ys = tuple((p[1] for p in points))

        coefficients = _Numeric.polyfit(xs, ys, N)

        bestFitPoints = []
        for x in xs:
            newY = 0.0
            power = len(coefficients)-1
            for coefficient in coefficients:
                newY += coefficient*(x**power)
                power -= 1
            bestFitPoints.append((x, newY))
           
        PolyLine.__init__(self, bestFitPoints, **attr)


Was there already a way to do this? If so, what is it! And if not, would
anyone be interested in having it included? If so I would be happy to
add another example to the pyplot demo showing an example of it and
submit a patch.

Bye for now,
- Mike Rooney


Reply | Threaded
Open this post in threaded view
|

Re: pyplot and best fit lines/polynomials?

Andrea Gavana
Hi Mike,

On 4/27/07, Mike Rooney wrote:

> I have been using pyplot a little and it sure is easy and quite fast!
> Recently I wanted to have a best-fit curve to my data and I couldn't
> find a built-in way to do this, so I added a little class to plot.py:
>
> class PolyBestFitLine(PolyLine):
>    """
>    Acts just like a PolyLine except that the Line
>    will be the best-fit polynomial of the points
>    given, with degree N.
>    """
>    def __init__(self, points, N=1, **attr):
>        xs = tuple((p[0] for p in points))
>        ys = tuple((p[1] for p in points))
>
>        coefficients = _Numeric.polyfit(xs, ys, N)
>
>        bestFitPoints = []
>        for x in xs:
>            newY = 0.0
>            power = len(coefficients)-1
>            for coefficient in coefficients:
>                newY += coefficient*(x**power)
>                power -= 1
>            bestFitPoints.append((x, newY))
>
>        PolyLine.__init__(self, bestFitPoints, **attr)
>
>
> Was there already a way to do this? If so, what is it! And if not, would
> anyone be interested in having it included? If so I would be happy to
> add another example to the pyplot demo showing an example of it and
> submit a patch.

I am not aware of any method in PyPlot that does that, so I believe it
would be an useful addition if you submit a patch. I also use PyPlot
quite often, for simple and fast plots, and I have already a couple of
apps in which I could add a "best line fit" option for the users :-D

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/