Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using ListDensityPlot to output the results of a physical simulation. I've passed both an array of values and coordinates coupled with magnitudes to ListDensityPlot.

Although I expect identical functionality it seems the points are interpolated differently.

I've recreated the problem in a very simple case in this code:

ListDensityPlot[{{1, 1, 2}, {2, 1, 2}, {3, 1, 1}, {4, 1, 1}, {1, 2, 1}, 
                 {2, 2, 1}, {3, 2, 1}, {4, 2, 1}}, InterpolationOrder -> 1]
ListDensityPlot[{{1, 2, 1, 1}, {1, 1, 1, 1}},      InterpolationOrder -> 1]

Toggling the third element of a list in the first function call from 1 to 2 causes a change (since all are 1), and changing any element in a list in the second function call from 1 to 2 does the same.

The input in the first call should be identical to that in the second. However, it seems that ListDensityPlot is using a different algorithm for smoothing in each case. I have tried altering the order in the first call, but this changes nothing.

Am I missing something or is this a known feature that I will have to work around?

share|improve this question

migrated from Jun 27 '12 at 18:05

This question came from our site for professional and enthusiast programmers.

Thank you to whoever migrated this topic. – Roberto Miguez Jul 9 '12 at 0:21
up vote 10 down vote accepted

I think it is just the order in which the points are fed to the algorithm:

       ListDensityPlot[{{1, 1, 2}, {2, 1, 1}, {1, 2, 1}, {2, 2, 1}}, Mesh -> All],
       ListDensityPlot[{{2, 1}, {1, 1}}, Mesh -> All]}}]

enter image description here

If you add a few more points, the effect of the points choosing sequence is diluted:

r = Table[{x, y, x + y}, {x, 10}, {y, 10}];
GraphicsGrid[{{ListDensityPlot[Flatten[r, 1], Mesh -> All],
               ListDensityPlot[r[[All, All, 3]], Mesh -> All]}}]

enter image description here


The problem behind the scenes is that in the first format {x,y, f[x,y]} Mma is calculating the nearest points to perform the interpolation, while in the second format Mma assumes a grid layout. When Mma calculates the euclidean distance between two points in your first case, as there are equal candidates, it chooses one that differs from the one used in the second algorithm.

You may tweak it if you need to:

me = $MachineEpsilon;
   ListDensityPlot[{{1, 1, 2}, {2 - me, 1, 1}, {1, 2, 1}, {2 - me, 2 - me, 1}}],
   ListDensityPlot[{{2, 1}, {1, 1}}]}}]

enter image description here

share|improve this answer
Excellent, much appreciated belisarius. – Roberto Miguez Jul 9 '12 at 0:23

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.