@@ -2621,7 +2621,7 @@ def newfunc(val, mask, mval):
2621
2621
if opened :
2622
2622
fh .close ()
2623
2623
2624
- def griddata (x ,y ,z ,xi ,yi ):
2624
+ def griddata (x ,y ,z ,xi ,yi , interp = 'nn' ):
2625
2625
"""
2626
2626
``zi = griddata(x,y,z,xi,yi)`` fits a surface of the form *z* =
2627
2627
*f*(*x*, *y*) to the data in the (usually) nonuniformly spaced
@@ -2633,7 +2633,8 @@ def griddata(x,y,z,xi,yi):
2633
2633
A masked array is returned if any grid points are outside convex
2634
2634
hull defined by input data (no extrapolation is done).
2635
2635
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
2637
2638
triangulation. By default, this algorithm is provided by the
2638
2639
:mod:`matplotlib.delaunay` package, written by Robert Kern. The
2639
2640
triangulation algorithm in this package is known to fail on some
@@ -2646,6 +2647,14 @@ def griddata(x,y,z,xi,yi):
2646
2647
algorithm, otherwise it will use the built-in
2647
2648
:mod:`matplotlib.delaunay` package.
2648
2649
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
+
2649
2658
The natgrid matplotlib toolkit can be downloaded from
2650
2659
http://sourceforge.net/project/showfiles.php?group_id=80706&package_id=142792
2651
2660
"""
@@ -2674,6 +2683,9 @@ def griddata(x,y,z,xi,yi):
2674
2683
y = y .compress (z .mask == False )
2675
2684
z = z .compressed ()
2676
2685
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." )
2677
2689
if xi .ndim == 2 :
2678
2690
xi = xi [0 ,:]
2679
2691
yi = yi [:,0 ]
@@ -2701,8 +2713,17 @@ def griddata(x,y,z,xi,yi):
2701
2713
# triangulate data
2702
2714
tri = delaunay .Triangulation (x ,y )
2703
2715
# 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'." )
2706
2727
# mask points on grid outside convex hull of input data.
2707
2728
if np .any (np .isnan (zo )):
2708
2729
zo = np .ma .masked_where (np .isnan (zo ),zo )
0 commit comments