constant spacing with griddata

Hi, I am trying to use griddata to interpolate some irregularly spaced data onto a linspace-created mesh in order to get a temperature plot.

I’ve been able to follow this example and it’s worked perfectly for me: http://matplotlib.sourceforge.net/examples/pylab_examples/griddata_demo.html

However, when I do my own data I get an error message that says:

Traceback (most recent call last):
File “make_colormap.py”, line 248, in
zi=griddata(x,y,z,xi,yi,interp=‘linear’)
File “/usr/lib/pymodules/python2.6/matplotlib/mlab.py”, line 2579, in griddata

raise ValueError("output grid must have constant spacing"

ValueError: output grid must have constant spacing when using interp=‘linear’

Looking into the documentation it seems that this is just suggesting that the xi,yi grids to be interpolated on have constant spacing. I don’t know if this is supposed to be the SAME constant spacing in xi and yi, but it appears not to be, since the example above uses the line:

xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,200)

so one is twice as fine as the other.

However, my code, when I do something like

xi=linspace(-20.1,20.1,100)
yi=linspace(-20.1,20.1,200)
zi=griddata(x,y,z,xi,yi,interp=‘linear’)
CS=plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)

it gives me the above error complaining about “constant spacing”.

Anyone have an idea what I might be missing here? I can turn interp to ‘nn’ and the code works fine, but I’m just curious what about a “constant spaced” output grid I don’t understand. Is it even possible to create a non-constant spaced output grid with linspace?

Thanks for the help!

Brad

Hi Brad,

Brad Malone, on 2011-12-19 19:06, wrote:

However, when I do my own data I get an error message that says:

> Traceback (most recent call last):
> File "make_colormap.py", line 248, in <module>
> zi=griddata(x,y,z,xi,yi,interp='linear')
> File "/usr/lib/pymodules/python2.6/matplotlib/mlab.py", line 2579, in
> griddata
> raise ValueError("output grid must have constant spacing"
> ValueError: output grid must have constant spacing when using
> interp='linear'

...

However, my code, when I do something like

> xi=linspace(-20.1,20.1,100)
> yi=linspace(-20.1,20.1,200)
> zi=griddata(x,y,z,xi,yi,interp='linear')
> CS=plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)

it gives me the above error complaining about "constant spacing".

Anyone have an idea what I might be missing here? I can turn interp to 'nn'
and the code works fine, but I'm just curious what about a "constant
spaced" output grid I don't understand. Is it even possible to create a
non-constant spaced output grid with linspace?

Looking at the code, it seems to be a floating point issue for
the way linspace works for the particular values you specified.
This should work for you, instead:

  xi=np.arange(-20.1,20.1+1e-14, 40.2/99)
  yi=np.arange(-20.1,20.1+1e-14,40.2/199)

NOTE: neither xi.max() nor yi.max() are "exactly" 20.1, the way
they were with linspace, nor is xi.max() exactly yi.max(). I say
"exactly" (in quotes) because when you type in 20.1, the floating point
number that you get isn't 20.10000000000000000000000000, it's
more like 20.10000000+change, in ipython, run '%precision 20' to
see what I mean.

Either the np.linspace or the mpl code likely needs to change,
because in mpl, the error gets raised when, effectively:

  dx = np.diff(xi)
  dx.max() - dx.min() > np.finfo(xi.dtype).resolution

and same for yi, which is what happens with the values you
provided to linspace, due to the way linspace works.

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7