From: John H. <jdh...@ac...> - 2005-03-31 14:54:53
|
>>>>> "Brian" == Brian B <b-m...@bb...> writes: Brian> Hello, I graph of weather data from a CGI script using the Brian> Agg backend only. The graph is a common time Brian> vs. temperature 2D line plot. Since the graph plots every n Brian> minutes, the lines on the graph tend to look pointed and Brian> not very aesthetically pleasing (as many people have Brian> informed me.) I did some research and found I needed to do Brian> curve fitting. Brian> I'm trying to use 8th order polynomial fitting. I have Brian> found a sample on the Web that shows how to use matplotlib Brian> to do a best-fit line from polyfit, but I want a curve that Brian> follows the curve of weather temperature data. Brian> I plot the data using a list of X-coordinates and a list of Brian> Y-coordinates. I assume I can call polyfit with (x,y,8) Brian> for 8th order polynomial fitting. However, I am not sure Brian> what to do with the results. I have tried to translate the Brian> 1st order polyfit example for my needs but I don't think I Brian> 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. Brian> Also, it has been suggested that a spline or Butterworth Brian> filter on the data may yield more predictable results. I Brian> have found some of this functionality in SciPy and a nifty Brian> module in a language called Octave. Would matplotlib Brian> 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() Brian> Any assistance is much appreciated. I'm just starting out Brian> on this type of stuff but it is fascinating to work with! Have fun! JDH |