3
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ ListPlot3D[xxx, RegionFunction-> Function[{z}, z<5]] $\endgroup$ – David G. Stork Jun 27 '18 at 19:33
  • 1
    $\begingroup$ 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). $\endgroup$ – Lukas Lang Jun 27 '18 at 19:35
  • $\begingroup$ Have you seen Select? $\endgroup$ – Alexei Boulbitch Jun 27 '18 at 19:44
  • 1
    $\begingroup$ 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. $\endgroup$ – David G. Stork Jun 27 '18 at 19:55
  • $\begingroup$ @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? $\endgroup$ – SarahThompson Jun 28 '18 at 10:54
2
$\begingroup$

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

enter image description here

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]

enter image description here

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

$\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.