11
$\begingroup$

Given a collection of scattered data points in 2D and associated function values,

data = RandomReal[1, {100, 3}];

we can get a piecewise linear interpolating function simply by calling Interpolation:

interp = Interpolation[data];
Plot3D[interp[x, y], {x, 0, 1}, {y, 0, 1}, PlotRange -> {0, 1}, 
 PlotPoints -> 50, ColorFunction -> "DarkRainbow"]

enter image description here

The result of Plot3D is essentially the same as calling ListPlot3D[data] directly. I imagine the piecewise linear elements are the Delaunay triangulation of the data points.

However, ListPlot3D also allows you to set InterpolationOrder -> 0 to get a plot that is piecewise constant over the Voronoi cells of the data points.

ListPlot3D[data, InterpolationOrder -> 0, PlotRange -> {0, 1}, 
 ColorFunction -> "DarkRainbow"]

enter image description here

I want to get the function corresponding to this plot, but Interpolation is of no help here:

Interpolation::udeg: Interpolation on unstructured grids is currently only supported for InterpolationOrder->1 or InterpolationOrder->All. Order will be reduced to 1.

Is there an easy way to get the result I want, that is, an interpolating function which is piecewise constant on the Voronoi cells of scattered data?

$\endgroup$
13
$\begingroup$

I think Nearest does it, but I'm having trouble getting a good plot.

SeedRandom[1]
data = RandomReal[1, {20, 3}];
func = Nearest[{#, #2} -> #3 & @@@ data];

Plot3D[func[{x, y}], {x, 0, 1}, {y, 0, 1}
 , PlotPoints -> 50
 , ColorFunction -> "DarkRainbow"
]

enter image description here

For comparison:

ListPlot3D[data
 , InterpolationOrder -> 0
 , PlotRange -> {{0, 1}, {0, 1}}
 , ColorFunction -> "DarkRainbow"
]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Oh sweet, that was easy! I don't really need a plot, so this is good enough for me. $\endgroup$ – user484 Dec 29 '12 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy