# Strange spikes in ListDensityPlot

I'm using the following dataset: https://drive.google.com/file/d/17-4oxD1czHAfwsz_svXVyjBrS5wMvmYc/view?usp=sharing

It consists of tiles defined by xmin, xmax, ymin, ymax and weight.

What I want to plot is ListDensityPlot of these tiles, so I first prep it (and scale it):

symlog[x_, thresh_] :=
If[x >= 0,
Log10[x + thresh] - Log10[thresh], -Log10[-x + thresh] +
Log10[thresh]];
threshold = 0.3;
xmin = -0.0355323;
xmax = 0.0432868;
ymin = -5.72074;
ymax = -1.33862;
min = 4.25438;
max = 7.61142;
contours2 =
Select[Import[
"mach1.txt",
"Table"], ((symlog[#[], threshold] <
xmax) && (symlog[#[], threshold] >
xmin) && (Log10[#[]] < ymax) && (Log10[#[]] > ymin)) &];
contours2 =
Table[{(symlog[contours2[[j, 1]], threshold] +
symlog[contours2[[j, 2]], threshold])/
2, (Log10[contours2[[j, 3]]] + Log10[contours2[[j, 4]]])/2,
Log[contours2[[j,
5]]/((contours2[[j, 2]] -
contours2[[j, 1]]) (contours2[[j, 4]] -
contours2[[j, 3]]))]}, {j, 1, Length@contours2}];


And now I plot it

ListDensityPlot[contours2,
PlotRange -> {{xmin - 0.01 (xmax - xmin),
xmax + 0.05 (xmax - xmin)}, {ymin - 0.05 (ymax - ymin),
ymax + 0.05 (ymax - ymin)}, {min, max}}, ImageSize -> 1000,
ColorFunction -> "Rainbow"]


And get a very weird, spiky plot (to be clear, I'm talking about the spikes in the red region. Some noise is expected in the lower region due to the natural lack of definition) I know for sure those spikes are a bug. To discover why, we plot it with zero interpolation order (that way we force Mathematica to plot the original tiles):

ListDensityPlot[contours2,
PlotRange -> {{xmin - 0.01 (xmax - xmin),
xmax + 0.05 (xmax - xmin)}, {ymin - 0.05 (ymax - ymin),
ymax + 0.05 (ymax - ymin)}, {min, max}}, ImageSize -> 1000,
ColorFunction -> "Rainbow", InterpolationOrder -> 0] Apart from the strange white spot, it is obvious something is going on here. To verify that the data is not to blame for the spikes, we plot these tiles via a different method. We get the original dataset, but this time manually draw the rectangles corresponding to the individual tiles (I know my way to display those individual tiles via dummy plot with epilog is silly, I don't know of any better way)

symlog[x_, thresh_] :=
If[x >= 0,
Log10[x + thresh] - Log10[thresh], -Log10[-x + thresh] +
Log10[thresh]];
threshold = 0.3;
xmin = -0.0355323;
xmax = 0.0432868;
ymin = -5.72074;
ymax = -1.33862;
min = 4.25438;
max = 7.61142;
contours2 =
Select[Import[
"mach1.txt",
"Table"], ((symlog[#[], threshold] <
xmax) && (symlog[#[], threshold] >
xmin) && (Log10[#[]] < ymax) && (Log10[#[]] > ymin)) &];
epilog = Table[{ColorData[
"Rainbow"][(1/(max - min)) (Log[
contours2[[j,
5]]/((contours2[[j, 2]] -
contours2[[j, 1]]) (contours2[[j, 4]] -
contours2[[j, 3]]))] - min)],
Rectangle[{symlog[contours2[[j, 1]], threshold],
Log10[contours2[[j, 3]]]}, {symlog[contours2[[j, 2]],
threshold], Log10[contours2[[j, 4]]]}]}, {j, 1,
Length@contours2}];
Plot[-100, {x, 0, 1},
PlotRange -> {{xmin - 0.01 (xmax - xmin),
xmax + 0.05 (xmax - xmin)}, {ymin - 0.05 (ymax - ymin),
ymax + 0.05 (ymax - ymin)}}, Axes -> None, Frame -> True,
AspectRatio -> 1, ImageSize -> 1000, Epilog -> epilog]


Now this produces the actual picture we should have obtained with ListDensityPlot with InterpolationOrder set to 0 There are no weird spikes in the actual data.

I could be using this plot as a density plot (I don't mind the roughness of the individual tiles), but I need Mathematica's internal routines to do it correctly, as I also want to extract the ListContourPlot of my data, which shows a similar bug It's obvious that the dataset has smooth contours, so these spiky contours are caused by the same bug that makes the ListDensityPlot so spiky.

What is behind this bug and how do I fix it?

• Try rescaling the data so that the height and width of the domain are approximately the same. Nov 4, 2021 at 14:12
• @MichaelE2 hey, thanks, that fixed it! Want to make it into an answer so I can upvote it? Is there a way to do it without rescaling? Nov 4, 2021 at 14:22
• You could post your answer. I don't mind. I don't have time to write it up, really. As for other workarounds, the problem is that the mesher assumes length is isotropic (the same in all directions). When the plot is stretched a lot more in one direction (by around a factor of 100 in your case), the jitter/interpolation error gets exaggerated. I don't know how set different scaling factors in the mesher. It may not be possible. Thus I don't have an alternative to suggest. Nov 4, 2021 at 15:31
• Try adding the option ScalingFunctions -> {{100 # &, #/100 &}, None} to ListDensityPlot. The scaling factor 100 may be adjusted if necessary. Nov 4, 2021 at 15:47

ListDensityPlot[contours2, 