10
$\begingroup$

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] 

enter image description here

It becomes better if you reorder the data.

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

enter image description here

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.

enter image description here

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Jun 15, 2016 at 13:38
  • $\begingroup$ 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}}]. ... $\endgroup$
    – Szabolcs
    Commented Jun 15, 2016 at 13:42
  • $\begingroup$ ... 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 ... $\endgroup$
    – Szabolcs
    Commented Jun 15, 2016 at 13:43
  • $\begingroup$ Interpolation uses the first format and ListInterpolation the second format. $\endgroup$
    – Szabolcs
    Commented Jun 15, 2016 at 13:45
  • $\begingroup$ @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. $\endgroup$
    – Sumit
    Commented Jun 15, 2016 at 13:54

1 Answer 1

6
$\begingroup$

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]

enter image description here

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]

enter image description here

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

$\endgroup$
1
  • $\begingroup$ That's an interesting point. I will take your word and wait for the universe to send more answers. $\endgroup$
    – Sumit
    Commented Jun 15, 2016 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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