Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2f00f4

Browse files
author
Jeff Whitaker
committed
add 'interp' keyword to griddata. Can be set to 'linear' to get faster
linear interpolation using Delaunay package. Default is 'nn' (natural neighbor). svn path=/trunk/matplotlib/; revision=7287
1 parent 9d85c4a commit c2f00f4

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2009-07-22 Added an 'interp' keyword to griddata so the faster linear
2+
interpolation method can be chosen. Default is 'nn', so
3+
default behavior (using natural neighbor method) is unchanged (JSW)
4+
15
2009-07-22 Improved boilerplate.py so that it generates the correct
26
signatures for pyplot functions. - JKS
37

lib/matplotlib/mlab.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,7 @@ def newfunc(val, mask, mval):
26212621
if opened:
26222622
fh.close()
26232623

2624-
def griddata(x,y,z,xi,yi):
2624+
def griddata(x,y,z,xi,yi,interp='nn'):
26252625
"""
26262626
``zi = griddata(x,y,z,xi,yi)`` fits a surface of the form *z* =
26272627
*f*(*x*, *y*) to the data in the (usually) nonuniformly spaced
@@ -2633,7 +2633,8 @@ def griddata(x,y,z,xi,yi):
26332633
A masked array is returned if any grid points are outside convex
26342634
hull defined by input data (no extrapolation is done).
26352635
2636-
Uses natural neighbor interpolation based on Delaunay
2636+
If interp keyword is set to '`nn`' (default),
2637+
uses natural neighbor interpolation based on Delaunay
26372638
triangulation. By default, this algorithm is provided by the
26382639
:mod:`matplotlib.delaunay` package, written by Robert Kern. The
26392640
triangulation algorithm in this package is known to fail on some
@@ -2646,6 +2647,14 @@ def griddata(x,y,z,xi,yi):
26462647
algorithm, otherwise it will use the built-in
26472648
:mod:`matplotlib.delaunay` package.
26482649
2650+
If the interp keyword is set to '`linear`', then linear interpolation
2651+
is used instead of natural neighbor. In this case, the output grid
2652+
is assumed to be regular with a constant grid spacing in both the x and
2653+
y directions. For regular grids with nonconstant grid spacing, you
2654+
must use natural neighbor interpolation. Linear interpolation is only valid if
2655+
:mod:`matplotlib.delaunay` package is used - :mod:`mpl_tookits.natgrid`
2656+
only provides natural neighbor interpolation.
2657+
26492658
The natgrid matplotlib toolkit can be downloaded from
26502659
http://sourceforge.net/project/showfiles.php?group_id=80706&package_id=142792
26512660
"""
@@ -2674,6 +2683,9 @@ def griddata(x,y,z,xi,yi):
26742683
y = y.compress(z.mask == False)
26752684
z = z.compressed()
26762685
if _use_natgrid: # use natgrid toolkit if available.
2686+
if interp != 'nn':
2687+
raise ValueError("only natural neighor interpolation"
2688+
" allowed when using natgrid toolkit in griddata.")
26772689
if xi.ndim == 2:
26782690
xi = xi[0,:]
26792691
yi = yi[:,0]
@@ -2701,8 +2713,17 @@ def griddata(x,y,z,xi,yi):
27012713
# triangulate data
27022714
tri = delaunay.Triangulation(x,y)
27032715
# interpolate data
2704-
interp = tri.nn_interpolator(z)
2705-
zo = interp(xi,yi)
2716+
if interp == 'nn':
2717+
interp = tri.nn_interpolator(z)
2718+
zo = interp(xi,yi)
2719+
elif interp == 'linear':
2720+
interp = tri.linear_interpolator(z)
2721+
zo = interp[yi.min():yi.max():complex(0,yi.shape[0]),
2722+
xi.min():xi.max():complex(0,xi.shape[1])]
2723+
else:
2724+
raise ValueError("interp keyword must be one of"
2725+
" 'linear' (for linear interpolation) or 'nn'"
2726+
" (for natural neighbor interpolation). Default is 'nn'.")
27062727
# mask points on grid outside convex hull of input data.
27072728
if np.any(np.isnan(zo)):
27082729
zo = np.ma.masked_where(np.isnan(zo),zo)

0 commit comments

Comments
 (0)