2
$\begingroup$

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

j132 = Import[
"/home/mateus/Desktop/LaminarSeparationBubble/AlamSandham/\
ShootingMethod/Rayleigh/Teste/Dados/New/paraloop_5.dat"];

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]

enter image description here

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

enter image description here

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.

enter image description here

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

$\endgroup$
  • 1
    $\begingroup$ Could you provide your j132 data? Have you tried with ListDensityPlot? $\endgroup$ – José Antonio Díaz Navas Dec 19 '17 at 20:04
  • $\begingroup$ 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. $\endgroup$ – halirutan Dec 20 '17 at 4:22
  • 2
    $\begingroup$ Possible duplicate of Stop ListContourPlot from interpolating beyond the data points $\endgroup$ – Edmund Dec 20 '17 at 11:25
  • $\begingroup$ You need to define a RegionFunction fro,m the points to use in ListContourPlot. $\endgroup$ – Edmund Dec 20 '17 at 11:26
  • 1
    $\begingroup$ @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. $\endgroup$ – halirutan Dec 20 '17 at 21:03
3
$\begingroup$

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]

Mathematica graphics

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]

Mathematica graphics

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]

Mathematica graphics

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]

Mathematica graphics

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

$\endgroup$
  • $\begingroup$ Could you make this work, too, by supplying a PlotRegion? Which you could probably build from a RegionMember on the appropriate MeshRegion. $\endgroup$ – b3m2a1 Jun 10 '18 at 2:21
  • $\begingroup$ @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. $\endgroup$ – halirutan Jun 10 '18 at 2:49
0
$\begingroup$

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)]
 ]

enter image description here

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}]]
   ]
 ]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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