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