# Use a symmetric mesh in ListDensityPlot

I am trying to produce a ListDensityPlot but I am having issues with aliasing. The problem arises from the way that Mathematica is breaking up the mesh into triangles, such that the orientations of the triangles is always in one direction, such that along one edge where there is a steep change in the function it appears smooth but along another edge is appears jagged. This is purely artificial due to the nature of the mesh, and could be solved if I could use a mesh which was somewhat more isotropic or which was axisymmetric about the center, but I cannot figure out how to do this. I'm surprised the mesh does not adapt where the derivative becomes large.

I am attaching two plots, with and without the mesh to demonstrate the issue. You can see aliasing on the top left and bottom right edges, whereas the top right and bottom left edges appear smooth. I am pretty certain this is caused by the mesh, as you can see in the second image the triangles are oriented differently along these different edges. The data set used to generate the plot is a simple square lattice. All help appreciated.

ListDensityPlot[data, ColorFunction -> "SunsetColors"] ListDensityPlot[data, ColorFunction -> "SunsetColors", Mesh->All] Edit:

Changing the interpolation order does not fix the issue, as per Henrik's response:

GraphicsGrid[
Partition[
Table[ListDensityPlot[data, ColorFunction -> "SunsetColors",
InterpolationOrder -> i], {i, 1, 6}],
3],
ImageSize -> Full] Data used to generate this plot can be found here or here.

• Would you please post the code that produced the plots? Feb 23, 2018 at 17:08
• You could try adding the option InterpolationOrder -> 3... Feb 23, 2018 at 17:13
• Can I attach a file with the data points?
– Kai
Feb 23, 2018 at 19:24
– Kai
Feb 23, 2018 at 19:40
• I just want to point out this is still apparently an issue in Mathematica 12
– Kai
Sep 6, 2019 at 18:31

Just a random example:

f = {x, y} \[Function] Sin[7 Pi (x + y)] + 0.7 Cos[12 (-1 x + 2 y + 1.)];
n = 25;
{x, y} = Transpose[Outer[List, Subdivide[0., 1., n], Subdivide[0., 1., n], 1], {2, 3, 1}];
data = f[x, y];
GraphicsGrid[
Partition[
Table[ListDensityPlot[data, InterpolationOrder -> i], {i, 1, 6}],
3], ImageSize -> Large
] So the mesh is not necessarily the issue.

Usually, the option MaxPlotPoints should help. But for some reason, it doesn't. So, let's upsample the image by hand: After download, we load the data, create an interpolation function and sample that on a finer grid.

data = Import["example_SF.mx"];
f = Interpolation[data, InterpolationOrder -> 3];
resolution = 400;
upsampledvals = Outer[f,
Subdivide[0., 4., resolution],
Subdivide[0., 4., resolution],
1];


Now we have the upsampled data but how to render it? Fortunately, @Wizard provided us in this answer with an efficient method to use Image with a costumized color pallete:

renderImage[array_?MatrixQ, cf_, q_Integer: 2048,
opts : OptionsPattern[Image]] :=
Module[{tbl},
tbl = List @@@ Array[cf, q, {0, 1}] // N //
DeveloperToPackedArray;
Image[tbl[[# + 1]] & /@ Round[(q - 1) array], opts]]


Applying it to the upsampled date leads to

img = renderImage[Rescale[upsampledvals], ColorData["SunsetColors"]] So far, this missed the axes. Those can be added like this:

Graphics[
Inset[img, {0, 0}, {0, 0}, {4, 4}],
Axes -> True,
Frame -> True,
PlotRange -> {{0, 4}, {0, 4}},
] • Hi, thanks for the reply, changing the interpolation order does not affect my results, I'm updating the question
– Kai
Feb 23, 2018 at 19:24
• Hi @Kai, I edited my answer and provided a workaround. However, I would classify that behavior at least as undesired and it might be worthwile to contact the support about it... Feb 23, 2018 at 20:30
• Thanks! I will definitely do that. I'm confused what you're doing in the second part with the axes? Also, the second image you provided seems be lower resolution than the first.
– Kai
Feb 24, 2018 at 0:01
• btw, I think you meant "But for some reason, it doesn't"
– Kai
Feb 24, 2018 at 0:17
• Oops. I corrected it. Towards the axes: I put the Image with Inset into the plain (with appropriate scaling) and just activate the axes (and some other cosmetics). Have a look at Inset in the documentation. Feb 24, 2018 at 0:22