griddata fails

Hi,

I'm trying to contour some data that I have and the griddata line fails. I tried running it on some synthetically generated data and I get the same IndexError. Any Ideas?

Here is the example with the synthetic data:

x = y = arange(-10,10,0.01)

z = x**2+y**3

xi = yi = linspace(-10.1, 10.1, 100)

zi = griddata(x, y, z, xi, yi)

···

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-52-0458ab6ea672> in <module>()
----> 1 zi = griddata(x, y, z, xi, yi)

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/mlab.py in griddata(x, y, z, xi, yi, interp)
   2766 xi,yi = np.meshgrid(xi,yi)
   2767 # triangulate data
-> 2768 tri = delaunay.Triangulation(x,y)
   2769 # interpolate data
   2770 if interp == 'nn':

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.py in __init__(self, x, y)
     88 self.triangle_neighbors = delaunay(self.x, self.y)
     89
---> 90 self.hull = self._compute_convex_hull()
     91
     92 def _collapse_duplicate_points(self):

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.py in _compute_convex_hull(self)
    113
    114 edges = {}
--> 115 edges.update(dict(zip(self.triangle_nodes[border[:,0]][:,1],
    116 self.triangle_nodes[border[:,0]][:,2])))
    117 edges.update(dict(zip(self.triangle_nodes[border[:,1]][:,2],

IndexError: invalid index

Hello Shahar,

I think that your simple example is probably not what you intended. Your (x,y) points are all defined on the straight line from (-10,-10) to (10,10). The Delaunay triangulation of these points (which is what griddata does) is not very interesting! Perhaps you wanted (x,y) defined on the 2D grid from (-10,-10) to (10,10), in which case you should follow the x = y … line with, for example:

x, y = meshgrid(x, y)

(see numpy.meshgrid for further details).

You may still obtain the same IndexError, and the traceback shows this is happening in the delaunay.Triangulation function call. The matplotlib delaunay package is not particularly robust, and can have problems handling regularly-spaced data points. The griddata documentation explains some of this, see http://matplotlib.org/api/mlab_api.html#matplotlib.mlab.griddata.

To avoid the problem, the griddata documentation explains one possible way that uses the natgrid algorithm. A simpler solution that I often use is to add a very small amount of noise to my regularly-spaced (x,y) points using the numpy.random module. I can give more details if you wish.

Ian

···

On 9 January 2013 09:32, Shahar Shani-Kadmiel <kadmiel@…4191…> wrote:

Hi,

I’m trying to contour some data that I have and the griddata line fails. I tried running it on some synthetically generated data and I get the same IndexError. Any Ideas?

Here is the example with the synthetic data:

x = y = arange(-10,10,0.01)

z = x2+y3

xi = yi = linspace(-10.1, 10.1, 100)

zi = griddata(x, y, z, xi, yi)


IndexError Traceback (most recent call last)

in ()

----> 1 zi = griddata(x, y, z, xi, yi)

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/mlab.py in griddata(x, y, z, xi, yi, interp)

2766 xi,yi = np.meshgrid(xi,yi)

2767 # triangulate data

-> 2768 tri = delaunay.Triangulation(x,y)

Yes, you are absolutely correct. I did not realize that I did not actually evaluate the function over a grid, makes sense that interpolation fails. I thought that since I created the two axis vectors the function evaluation occurs over the entire domain, meshgrid is what I was missing.

thanks,

Shahar

···

On Jan 9, 2013, at 8:45 PM, Ian Thomas wrote:

On 9 January 2013 09:32, Shahar Shani-Kadmiel <kadmiel@…4191…> wrote:

Hi,

I’m trying to contour some data that I have and the griddata line fails. I tried running it on some synthetically generated data and I get the same IndexError. Any Ideas?

Here is the example with the synthetic data:

x = y = arange(-10,10,0.01)

z = x2+y3

xi = yi = linspace(-10.1, 10.1, 100)

zi = griddata(x, y, z, xi, yi)


IndexError Traceback (most recent call last)

in ()

----> 1 zi = griddata(x, y, z, xi, yi)

/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/mlab.py in griddata(x, y, z, xi, yi, interp)

2766 xi,yi = np.meshgrid(xi,yi)

2767 # triangulate data

-> 2768 tri = delaunay.Triangulation(x,y)

Hello Shahar,

I think that your simple example is probably not what you intended. Your (x,y) points are all defined on the straight line from (-10,-10) to (10,10). The Delaunay triangulation of these points (which is what griddata does) is not very interesting! Perhaps you wanted (x,y) defined on the 2D grid from (-10,-10) to (10,10), in which case you should follow the x = y … line with, for example:

x, y = meshgrid(x, y)

(see numpy.meshgrid for further details).

You may still obtain the same IndexError, and the traceback shows this is happening in the delaunay.Triangulation function call. The matplotlib delaunay package is not particularly robust, and can have problems handling regularly-spaced data points. The griddata documentation explains some of this, see http://matplotlib.org/api/mlab_api.html#matplotlib.mlab.griddata.

To avoid the problem, the griddata documentation explains one possible way that uses the natgrid algorithm. A simpler solution that I often use is to add a very small amount of noise to my regularly-spaced (x,y) points using the numpy.random module. I can give more details if you wish.

Ian