# Plotting region from list

I have what should be a rather simple problem, but I am struggling with an elegant solution.

I have a nested list of values of the form {{x1,y1,p1},{x2,y2,p2},...{xn,yn,pn}} where p corresponds to the variable pair x and y.

Ultimately, I want to make RegionPlots on the y-x plane of the points for which p < some value, i.e., I want to make a plot of the region where p < 5, then do the same for p < 7, etc. Ideally I would like to plot these regions on the same plot, with different colored dots depending on the p-value.

I am struggling with working with the nested list (despite having looked through the documentation), and having RegionPlot refer to particular list elements and compare with the third element of each nested set. Any help would be much appreciated.

• ListPlot3D[xxx, RegionFunction-> Function[{z}, z<5]] Jun 27, 2018 at 19:33
• I think you're looking for ListContourPlot. It accepts data in exactly your format, and you can easily specify which contours to draw using Contours (i.e. which regions). Jun 27, 2018 at 19:35
• Have you seen Select? Jun 27, 2018 at 19:44
• Select will allow you to select the data that obeys some constraint, but if you use RegionPlot or simple ListPlot3D, etc., you will likely get the full range of positions, not restricted to where the data lie. Jun 27, 2018 at 19:55
• @AlexeiBoulbitch - Thanks, have had a look at that. I guess in my case it would be something like Select[list, 5 < Last[#] < 7 &], although I am not sure how to pass this into a RegionPlot argument? Jun 28, 2018 at 10:54

As mentioned in the comments, regions based on the third column can be plotted using ListContourPlot:

SeedRandom[1]
dt = RandomReal[10, {100, 3}];

pbins = {2, 5, 7};
colors = ColorData[97, "ColorList"];

lcp =ListContourPlot[dt, ImageSize -> 500, PlotLegends -> Automatic,
InterpolationOrder -> 0, Contours -> pbins,
ContourShading -> (Opacity[.5, #] & /@ colors) ]


Grouping data based on the third column can be done using a combination of Nearest and GroupBy or using HistogramList with a custom height function to bin data by the the third column.

### Nearest + GroupBy

pbins = {2, 5, 7};
binlims = {Floor[Min[dt[[All, -1]]]] - $MachineEpsilon, ## & @@ pbins,$MachineEpsilon + Ceiling[Max[dt[[All, -1]]]]} ;
nf = Nearest[binlims -> "Index"];
parentBin = If[binlims[[First@nf@#]] <= #,
binlims[[{#, # + 1} &@First@nf[#]]],
binlims[[{# - 1, #} &@First@nf[#]]] ] &;
bl1 = Values @ GroupBy[SortBy[dt, Last], parentBin[Last@#] & -> (#[[;; 2]] &)];
lp1 = ListPlot[bl1, PlotStyle -> PointSize[Large]];
Show[lcp, lp1]


### HistogramList

binspecs = {1, 1, {binlims}};
heightfunc = BinLists[dt, {#[[1]]}, {#[[2]]}, {#[[3]]}] &;
bl2 = HistogramList[dt, binspecs, heightfunc][[2, 1, 1]][[All, All, ;; 2]];
Sort /@ bl2 == Sort/@ bl1


True

lp2 = ListPlot[bl2, PlotStyle -> PointSize[Large]] ;
Show[lcp, lp2]


same picture