Polyfit help

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

Dear John

When running the tex_demo.py script I just get a gray
figure canvas (no curves or anything) and the
following error message:

···

__________________________________________________
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345,
in __call__
    return self.func(*args)
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_tkagg.py",
line 148, in resize
    self.show()
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_tkagg.py",
line 151, in draw
    FigureCanvasAgg.draw(self)
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_agg.py",
line 381, in draw
    self.figure.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\figure.py",
line 511, in draw
    for a in self.axes: a.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\axes.py",
line 1387, in draw
    self.xaxis.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\axis.py",
line 552, in draw
    tick.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\axis.py",
line 151, in draw
    if self.label1On: self.label1.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\text.py",
line 848, in draw
    self._mytext.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\text.py",
line 335, in draw
    bbox, info = self._get_layout(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\text.py",
line 184, in _get_layout
    w,h = renderer.get_text_width_height(
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_agg.py",
line 241, in get_text_width_height
    Z = self.texmanager.get_rgba(s, size, dpi, rgb)
  File
"C:\Python24\Lib\site-packages\matplotlib\texmanager.py",
line 296, in get_rgba
    X = readpng(pngfile)
RuntimeError: _image_module::readpng could not open
PNG file C:\Documents and
Settings\Kristen\.matplotlib\tex.cache\30565a8911a6bb487e3745c0ea3c8224_96.png
for reading
______________________________________________________

Any idea of what I'm missing??

Kristen

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Not really, but I wonder if it is related to my problem
below. Both look like a temp file cannot be found.
I am on Win2000; how about you?
Alan Isaac

Traceback (most recent call last):
  File "C:\temp8.py", line 30, in ?
    savefig('/temp.eps')
  File "C:\Python24\Lib\site-packages\matplotlib\pylab.py", line 773, in savefig
    return fig.savefig(*args, **kwargs)
  File "C:\Python24\Lib\site-packages\matplotlib\figure.py", line 636, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "C:\Python24\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 179, in print_figure
    agg.print_figure(filename, dpi, facecolor, edgecolor, orientation)
  File "C:\Python24\Lib\site-packages\matplotlib\backends\backend_agg.py", line 474, in print_figure
    ps.print_figure(filename, dpi, facecolor, edgecolor, orientation)
  File "C:\Python24\Lib\site-packages\matplotlib\backends\backend_ps.py", line 1103, in print_figure
    shutil.move(epsfile, outfile)
  File "C:\Python24\lib\shutil.py", line 189, in move
    copy2(src,dst)
  File "C:\Python24\lib\shutil.py", line 92, in copy2
    copyfile(src, dst)
  File "C:\Python24\lib\shutil.py", line 47, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: '80bc875819e5bf1b449889aadd91b3af.eps'

···

On Sat, 30 Jul 2005, kristen kaasbjerg apparently wrote:

RuntimeError: _image_module::readpng could not open
PNG file C:\Documents and
Settings\Kristen\.matplotlib\tex.cache\30565a8911a6bb487e3745c0ea3c8224_96.png
for reading
______________________________________________________

Any idea of what I'm missing??

Hi Kristen,
Last time I looked (a couple of weeks ago), there were problems in this
area of matplotlib with LaTeX not being invoked properly.
Can you please tell us what version of Windows, matplotlib and which
LaTeX distribution you are using.
I was intending to have another look at the code in a week or so from
now. I'm unable to spend time on it until then. However, it looks like
someone has made more recent changes to the relevant code, so I'd
encourage you to try the latest version of matplotlib if the version
you're using is more that a couple of weeks old. You may find that the
problem you are seeing has been fixed.
Please report back if you have success after doing this.
Gary R.

kristen kaasbjerg wrote:

···

Dear John

When running the tex_demo.py script I just get a gray
figure canvas (no curves or anything) and the
following error message:
__________________________________________________
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345,
in __call__
    return self.func(*args)
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_tkagg.py",
line 148, in resize
    self.show()
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_tkagg.py",
line 151, in draw
    FigureCanvasAgg.draw(self)
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_agg.py",
line 381, in draw
    self.figure.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\figure.py",
line 511, in draw
    for a in self.axes: a.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\axes.py",
line 1387, in draw
    self.xaxis.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\axis.py",
line 552, in draw
    tick.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\axis.py",
line 151, in draw
    if self.label1On: self.label1.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\text.py",
line 848, in draw
    self._mytext.draw(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\text.py",
line 335, in draw
    bbox, info = self._get_layout(renderer)
  File
"C:\Python24\Lib\site-packages\matplotlib\text.py",
line 184, in _get_layout
    w,h = renderer.get_text_width_height(
  File
"C:\Python24\Lib\site-packages\matplotlib\backends\backend_agg.py",
line 241, in get_text_width_height
    Z = self.texmanager.get_rgba(s, size, dpi, rgb)
  File
"C:\Python24\Lib\site-packages\matplotlib\texmanager.py",
line 296, in get_rgba
    X = readpng(pngfile)
RuntimeError: _image_module::readpng could not open
PNG file C:\Documents and
Settings\Kristen\.matplotlib\tex.cache\30565a8911a6bb487e3745c0ea3c8224_96.png
for reading
______________________________________________________

Any idea of what I'm missing??

Kristen

Sascha found some problems with the way commands were being passed, which only
effected windows. I commited her patches.

Sascha, could you comment?.

···

On Saturday 30 July 2005 08:52 am, Gary Ruben wrote:

Hi Kristen,
Last time I looked (a couple of weeks ago), there were problems in this
area of matplotlib with LaTeX not being invoked properly.
Can you please tell us what version of Windows, matplotlib and which
LaTeX distribution you are using.
I was intending to have another look at the code in a week or so from
now. I'm unable to spend time on it until then. However, it looks like
someone has made more recent changes to the relevant code, so I'd
encourage you to try the latest version of matplotlib if the version
you're using is more that a couple of weeks old. You may find that the
problem you are seeing has been fixed.
Please report back if you have success after doing this.
Gary R.

How does Matplotlib find TeX?
Can it be directed to a specific distribution?

Thanks,
Alan

···

On Sat, 30 Jul 2005, Gary Ruben apparently wrote:

Last time I looked (a couple of weeks ago), there were problems in this
area of matplotlib with LaTeX not being invoked properly.

Hi Gary

I'm using windows XP, matplotlib 0.83.2, python 2.4
and latex2e.

Kristen

Hi Kristen,
Last time I looked (a couple of weeks ago), there
were problems in this
area of matplotlib with LaTeX not being invoked
properly.
Can you please tell us what version of Windows,
matplotlib and which
LaTeX distribution you are using.
I was intending to have another look at the code in
a week or so from
now. I'm unable to spend time on it until then.
However, it looks like
someone has made more recent changes to the relevant
code, so I'd
encourage you to try the latest version of
matplotlib if the version
you're using is more that a couple of weeks old. You
may find that the
problem you are seeing has been fixed.
Please report back if you have success after doing
this.
Gary R.

kristen kaasbjerg wrote:
> Dear John
>
> When running the tex_demo.py script I just get a
gray
> figure canvas (no curves or anything) and the
> following error message:
> __________________________________________________
> Exception in Tkinter callback
> Traceback (most recent call last):
> File "C:\Python24\lib\lib-tk\Tkinter.py", line
1345,
> in __call__
> return self.func(*args)
> File
>

"C:\Python24\Lib\site-packages\matplotlib\backends\backend_tkagg.py",

> line 148, in resize
> self.show()
> File
>

"C:\Python24\Lib\site-packages\matplotlib\backends\backend_tkagg.py",

> line 151, in draw
> FigureCanvasAgg.draw(self)
> File
>

"C:\Python24\Lib\site-packages\matplotlib\backends\backend_agg.py",

> line 381, in draw
> self.figure.draw(renderer)
> File
>

"C:\Python24\Lib\site-packages\matplotlib\figure.py",

> line 511, in draw
> for a in self.axes: a.draw(renderer)
> File
>
"C:\Python24\Lib\site-packages\matplotlib\axes.py",
> line 1387, in draw
> self.xaxis.draw(renderer)
> File
>
"C:\Python24\Lib\site-packages\matplotlib\axis.py",
> line 552, in draw
> tick.draw(renderer)
> File
>
"C:\Python24\Lib\site-packages\matplotlib\axis.py",
> line 151, in draw
> if self.label1On: self.label1.draw(renderer)
> File
>
"C:\Python24\Lib\site-packages\matplotlib\text.py",
> line 848, in draw
> self._mytext.draw(renderer)
> File
>
"C:\Python24\Lib\site-packages\matplotlib\text.py",
> line 335, in draw
> bbox, info = self._get_layout(renderer)
> File
>
"C:\Python24\Lib\site-packages\matplotlib\text.py",
> line 184, in _get_layout
> w,h = renderer.get_text_width_height(
> File
>

"C:\Python24\Lib\site-packages\matplotlib\backends\backend_agg.py",

> line 241, in get_text_width_height
> Z = self.texmanager.get_rgba(s, size, dpi,
rgb)
> File
>

"C:\Python24\Lib\site-packages\matplotlib\texmanager.py",

> line 296, in get_rgba
> X = readpng(pngfile)
> RuntimeError: _image_module::readpng could not
open
> PNG file C:\Documents and
>

Settings\Kristen\.matplotlib\tex.cache\30565a8911a6bb487e3745c0ea3c8224_96.png

···

--- Gary Ruben <gruben@...636...> wrote:

> for reading
>

______________________________________________________

>
> Any idea of what I'm missing??
>
> Kristen

-------------------------------------------------------

SF.Net email is sponsored by: Discover Easy Linux
Migration Strategies
from IBM. Find simple to follow Roadmaps,
straightforward articles,
informative Webcasts and more! Get everything you
need to get up to
speed, fast.

http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net

____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs

Hi Kristen,

Looks like you're using the latest version of the relevant file from cvs, so I'm not sure what's going on. We'll have to hope for comment from the others. I just wanted to say thanks for that version information which may help me when I look at this stuff again in a couple of weeks. I hope others can sort out your problem in the meantime.

Gary R.

kristen kaasbjerg wrote:

Hi Gary

I'm using windows XP, matplotlib 0.83.2, python 2.4
and latex2e.

Kristen

<snip>

Dear John

When running the tex_demo.py script I just get a gray
figure canvas (no curves or anything) and the
following error message:

[...]

"C:\Python24\Lib\site-packages\matplotlib\texmanager.py",
line 296, in get_rgba
    X = readpng(pngfile)
RuntimeError: _image_module::readpng could not open
PNG file C:\Documents and
Settings\Kristen\.matplotlib\tex.cache\30565a8911a6bb487e3745c0ea3c8224_96.
png for reading
______________________________________________________

Any idea of what I'm missing??

Do you have dvipng installed?

···

On Saturday 30 July 2005 06:18 am, kristen kaasbjerg wrote:

--

Darren

Hi matplotliber's (windows users)

A few days ago I reported problems with the
tex_demo.py script. Some solutions have been found
meanwhile.

First of all, matplotlibrc is NOT put in the intended
directory upon istallation. It resides in the old
share\matplotlib\. This must be done manually!!

Secondly, when using TeX to create figure text, make
sure that the directories where dvipng.exe, latex.exe,
gs.exe etc are present in the system variable Path.
For most latex/miktex installation this will probably
be: C:\texmf\miktex\bin and C:\gs\.

I have still not made it work using the PS backend.
Somewhere there is an eps and tex file that has not
been put in the .matplotlib\tex.cache directory.

Also, much of the text doesn't come out correctly in
the figures, so there are still problems when using
TeX on Windows!!

Kristen

···

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Hi Kristen,

The PS backend is broken on my Win2k system too. I can only save as png, which isn't all that useful. I reported this a few weeks ago and I hope to get a chance to look at it next week to see if I can work out what's going on.
It's good to get confirmation that it's not just my Windows installation that's doing this.

Gary R.

kristen kaasbjerg wrote:

···

Hi matplotliber's (windows users)

A few days ago I reported problems with the
tex_demo.py script. Some solutions have been found
meanwhile.

First of all, matplotlibrc is NOT put in the intended
directory upon istallation. It resides in the old
share\matplotlib\. This must be done manually!!

Secondly, when using TeX to create figure text, make
sure that the directories where dvipng.exe, latex.exe,
gs.exe etc are present in the system variable Path.
For most latex/miktex installation this will probably
be: C:\texmf\miktex\bin and C:\gs\.

I have still not made it work using the PS backend.
Somewhere there is an eps and tex file that has not
been put in the .matplotlib\tex.cache directory.

Also, much of the text doesn't come out correctly in
the figures, so there are still problems when using
TeX on Windows!!

Kristen

Do you mean ps is broken with TeX support, or it is broken period?

I will defend in a couple of weeks, so unfortunately I don't have time to bug
hunt for a while. I should be back on the ball soon.

Darren

···

On Monday 01 August 2005 08:36 am, Gary Ruben wrote:

The PS backend is broken on my Win2k system too. I can only save as png,
which isn't all that useful. I reported this a few weeks ago and I hope
to get a chance to look at it next week to see if I can work out what's
going on.
It's good to get confirmation that it's not just my Windows installation
that's doing this.

Darren Dale wrote:
<snip>
> Do you mean ps is broken with TeX support, or it is broken period?

Just the TeX->ps support is broken.

···

On Monday 01 August 2005 08:36 am, Gary Ruben wrote: