Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 11 down vote accepted

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

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

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

Plot3D[func[{x, y}], {x, 0, 1}, {y, 0, 1}, PlotPoints -> 50, 
 ColorFunction -> (Hue@#3 &)]

Mathematica graphics

For comparison:

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

Mathematica graphics

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.