# Is there a way to change the interpolation scheme used in ListDensityPlot?

Recently I was trying to answer this question on how to make a smooth ListDensityPlot when I found that the output depends on the order of the data if you are using InterpolationOrder>0. For example

data = Flatten[Table[{x, y, If[x==y, 1, 0]}, {x, 0, 1, 1/10}, {y, 0, 1, 1/10}],1];
ListDensityPlot[data]


It becomes better if you reorder the data.

data2 = Sort[data, #1[[1]] < #2[[1]] &];
ListDensityPlot[data2]


Instead of changing the data, I guess the same result could be obtained if we can change the interpolation scheme MMA is using. Is there any way to do that?

By interpolation scheme I mean the algorithm MMA is using to produce the data in between two data points to make a smooth figure.

Just for curiosity, I try the same thing with M9.0.1 and this is the result with data2.

• Both use linear interpolation. In 2D that works on triangles. So even though you are using a square grid, Mathematica breaks each square into two triangles. There are two ways to do that for each square. Use Mesh -> All to see what is happening. – Szabolcs Jun 15 '16 at 13:38
• About changing the interpolation scheme: you can set the InterpolationOrder option. There are two kinds of data ListDensityPlot (or Interpolation itself) can work with: specified as {x,y,f[x,y]} triplets, like you did, or a matrix of values. You could do this as well: gridData = Table[If[x == y, 1, 0], {x, 0, 1, 1/10}, {y, 0, 1, 1/10}]; ListDensityPlot[gridData, Mesh -> All, DataRange -> {{0, 1}, {0, 1}}]. ... – Szabolcs Jun 15 '16 at 13:42
• ... With the first format, you can only use order 0 or 1. With the second format you can use any order. The weird thing is that with the second format Mathematica does something very strange if setting the order to be 1, even though the default should be one: ListDensityPlot[gridData, Mesh -> All, DataRange -> {{0, 1}, {0, 1}}, InterpolationOrder -> 1] I don't know why this happens. Looks like a bug ... – Szabolcs Jun 15 '16 at 13:43
• Interpolation uses the first format and ListInterpolation the second format. – Szabolcs Jun 15 '16 at 13:45
• @Szabolcs, could you make it an answer? That would be helpful for further discussion. And I think the default InterpolationOrder is 3. I have to check that. It is the direction of interpolation I am interested in. – Sumit Jun 15 '16 at 13:54

ListDensityPlot interpolated by constructing a Delaunay triangulation of the data points and interpolating linearly on each triangle. For a perfectly rectangular point grid, there are two ways to break each rectangle into triangles.

If we distort the grid slightly and make each rectangle a parallelogram, then there will only be one way to construct the Delaunay triangulation.

So we can do this:

data = Flatten[
Table[{x, y + 0.0001 x, If[x == y, 1, 0]}, {x, 0, 1, 1/10}, {y, 0,
1, 1/10}], 1];
ListDensityPlot[data, Mesh -> All]


data = Flatten[
Table[{x, y - 0.0001 x, If[x == y, 1, 0]}, {x, 0, 1, 1/10}, {y, 0,
1, 1/10}], 1];
ListDensityPlot[data, Mesh -> All]


This is a hack so you might want to wait for other answers ...

• That's an interesting point. I will take your word and wait for the universe to send more answers. – Sumit Jun 15 '16 at 14:47