Hello, I graph of weather data from a CGI script using the

> Agg backend only. The graph is a common time

> vs. temperature 2D line plot. Since the graph plots every n

> minutes, the lines on the graph tend to look pointed and

> not very aesthetically pleasing (as many people have

> informed me.) I did some research and found I needed to do

> curve fitting.

> I'm trying to use 8th order polynomial fitting. I have

> found a sample on the Web that shows how to use matplotlib

> to do a best-fit line from polyfit, but I want a curve that

> follows the curve of weather temperature data.

> I plot the data using a list of X-coordinates and a list of

> Y-coordinates. I assume I can call polyfit with (x,y,8)

> for 8th order polynomial fitting. However, I am not sure

> what to do with the results. I have tried to translate the

> 1st order polyfit example for my needs but I don't think I

> am using the polyfit data correctly.

You would need to use polyval to get the results of polyfit (there is

an example in the matplotlib Users Guide in the Cookbook chapter for a

3rd order fit), but I don't think you want to use an 8-th order

polynomial for this -- as you indicate below, a spline or a filter is

a better choice.

> Also, it has been suggested that a spline or Butterworth

> filter on the data may yield more predictable results. I

> have found some of this functionality in SciPy and a nifty

> module in a language called Octave. Would matplotlib

> benefit from this?

Use spline if you want a curve that passes through all your data, use

butterworth or convolution if you want to smooth your data.

scipy is your best bet -- scipy spline and a butterworth filter

examples from my scipy examples directory are included. In general,

we try to stay focused on plotting in matplotlib rather than

algorithms, and leave algorithms to the scipy folks. They are working

hard on getting a modular package that is easy to install. I think it

would be useful to provide some wrappers around scipy in the

matplotlib.mlab module that exposed a matlab interface to some of

their algorithms, with imports done in such a way that having the

additional scipy functionality would be optional

Here is a scipy spline example, plotted with mpl

from scipy import arange, sin, pi, interpolate

from pylab import plot, show

# Cubic-spline

t = arange(0, 2.0, 0.1)

y = sin(2*pi*t)

tck = interpolate.splrep(t, y, s=0)

tnew = arange(0, 2.0, 0.01)

ynew = interpolate.splev(tnew, tck, der=0)

plot(t, y, 'o', tnew, ynew)

show()

And here is a butterworth filter. Note that filters can introduce

phase shifts in your data (illustrated in this example) so use with

caution!

from __future__ import division

from scipy import signal, arange, sin, pi, linspace, transpose

from RandomArray import normal

from pylab import plot, show, subplot

from scipy.signal import buttord, butter, lfilter

dt = 0.001

t = arange(0.0, 10.0, dt)

nse = normal(0.0, 0.1, t.shape)

#s =

s = normal(0.0, 1.0, (len(t),22))

for i in range(22):

s[:,i] += sin(2*pi*t)

lpcf = 3

lpsf = 5

Nyq = 1/(2*dt)

Rp = 2

Rs = 20

Wp = lpcf/Nyq

Ws = lpsf/Nyq

[n,Wn] = buttord(Wp,Ws,Rp,Rs)

[b,a] = butter(n,Wn)

xlp = transpose(lfilter(b,a,transpose(s)))

subplot(311)

plot(t, s[:,0])

subplot(312)

plot(t, xlp[:,0])

subplot(313)

plot(t, xlp[:,1])

show()

> Any assistance is much appreciated. I'm just starting out

> on this type of stuff but it is fascinating to work with!

Have fun!

JDH