# ListContourPlot Interpolation

I plotted a data file with ListContourPlot and I have this result:

j132 = Import[
"/home/mateus/Desktop/LaminarSeparationBubble/AlamSandham/\

lista132 = Table[{Abs[j132[[i, 3]]], j132[[i, 4]], j132[[i, 8]]}, {i,
1,Length[j132]}];

ListContourPlot[lista132, Contours -> 50, PlotLegends -> Automatic,
FrameLabel -> {"\!$$\*SubscriptBox[\(U$$, $$R$$]\)",
"\!$$\*SubscriptBox[\(h$$, $$R$$]\)",
"\!$$\*SubscriptBox[\(\[Omega]$$, $$i$$]\)"},
FrameStyle -> FontSize -> 14, ImageSize -> 350]


But, if I plot the same file data with "mesh" function at MATLAB I have this result:

It's seems that Mathematica interpolates some region. Look the region below and above at the first figure. MATLAB doesn't interpolate. Maybe the next figure is more clear.

So the questions is: How can I obtain the same result of MATLAB using Mathematica?

• Could you provide your j132 data? Have you tried with ListDensityPlot? – José Antonio Díaz Navas Dec 19 '17 at 20:04
• Without an example data set, I usually wouldn't upvote this question. However, I believe the topic is interesting and I hope some other answer will pop up. Therefore +1. Feel free to include my test data in your question. – halirutan Dec 20 '17 at 4:22
• Possible duplicate of Stop ListContourPlot from interpolating beyond the data points – Edmund Dec 20 '17 at 11:25
• You need to define a RegionFunction fro,m the points to use in ListContourPlot. – Edmund Dec 20 '17 at 11:26
• @Edmund This is odd because it was the first thing I tried. Defining a region function that based on a NearestFunction to the points. Maybe I missed something, but I couldn't get a density plot without the non-convex part. – halirutan Dec 20 '17 at 21:03

What you did in Matlab is not to compare what ListDensityPlot (which is what you should rather use) does. The white spots inside the plot tell that Matlab is not plotting a polygonized area, but a set of colored points instead.

Some test data that is comparable and consists of {x,y,value} entries:

data = Function[{x, y, z}, {x, y + x^2, z}] @@@
Flatten[Table[{x, y, Sin[x + y]}, {x, -Pi, Pi, .1}, {y, 0, 2 Pi, .1}], 1];


First, let us kind-of recreate what Matlab does

Graphics[{ColorData["SunsetColors", #3^2], Point[{#1, #2}]} & @@@
data, Frame -> True, FrameTicks -> True]


You can close the gaps by making the points larger, but in general this is no good solution when your data points are irregular. ListDensityPlot gives a smooth area instead that consists of an interpolated region of colored polygons

ListDensityPlot[data]


The problem is that the mesh generation will close regions when they are not convex. This might seem confusing at first, but it comes down to that there is absolutely no way to tell if the upper part is not part of the region or if you just miss some data-points there. In general, this answer cannot be answered and therefore, Mathematica assumes a convex region and closes it.

However, you might be able to simply hack around this. Let us look at the created mesh

ListDensityPlot[data, Mesh -> All]


In this example, the region where points are packed together can be separated from the region with long polygon edges and since I know that this part should not be in the graphics, I can filter out the large polygons

DeleteCases[Normal[ListDensityPlot[data]],
Polygon[pts_, ___] /;
Max[Norm[Subtract @@ #] & /@ Partition[pts, 2, 1]] > .6, Infinity]


With some adaption, this might as well work for your particular data set.

• Could you make this work, too, by supplying a PlotRegion? Which you could probably build from a RegionMember on the appropriate MeshRegion. – b3m2a1 Jun 10 '18 at 2:21
• @b3m2a1 Here this approach works very nice. However, if you only have points, you first need to construct the shape of the region and this is not easily possible. For humans, it's easy to look at scattered points and draw a region boundary, for an algorithm it's not so easy. I have seen several implementations of such algorithms in MeshLab, but I'm not sure we can do this in Mathematica out of the box. – halirutan Jun 10 '18 at 2:49

Here's another approach, making use of RegionFunction and halirutan's data:

ListDensityPlot[
data2,
RegionFunction ->
Function[{x, y, z}, y >= x^2 && y <= (1.9735 π + x^2)]
]


For some bizarre reason it doesn't work with a proper 2 π...

If you want to generalize this to nastier shapes you can use any arbitrary Region and then supply the function that RegionMember makes, e.g.:

regionFunction =
RegionMember@
ImplicitRegion[
y >= x^2 && y <= 1.9735 π + x^2,
{{x, -3, 3}, {y, 0, 15}}
];

ListDensityPlot[
data2,
RegionFunction ->
Function[
Evaluate[regionFunction[{#, #2}]]
]
]