3
$\begingroup$

I've hit a problem using RegionFunction with ListContourPlot and am hoping someone can help.

I'm trying to use RegionFunction with ListContourPlot to suppress the rendering of contours outside a prescribed polygon.

The first example in this notebook works as I'd expect. The second does not, in that the contours are drawn outside the specified polygon.

Any suggestions/advice would be much appreciated.

$\endgroup$
1
  • $\begingroup$ Crossposted here. $\endgroup$ Commented Mar 21, 2023 at 15:47

2 Answers 2

2
$\begingroup$

First, precompute RegionMemberFunction for better performance. Second, increase MaxPlotPoints.

regMem = RegionMember[Polygon[...]];

Show[ListContourPlot[..., 
   RegionFunction -> Function[{x, y, z}, regMem[{x, y}]], 
   MaxPlotPoints -> 50]
]

ListContourPlot

Alternatively, if you don't want all of your points to get additionally resampled at a grid (which is what increasing MaxPlotPoints does and can be observed with Mesh -> All), you can trick Mathematica by providing RegionFunction of a region which is very slightly smaller than your actual region of datapoints. This will trigger finer resampling only at the boundary (observe with mesh). I do not know whether this can also be achieved in any other way ...

reg = Polygon[...];
smallerReg = 
  GeometricTransformation[reg, 
   ScalingTransform[{1 - 1/100000, 1 - 1/100000}, RegionCentroid[reg]]];
regMem = RegionMember[smallerReg];

Show[ListContourPlot[..., 
   RegionFunction -> Function[{x, y, z}, regMem[{x, y}]]
]

ListContourPlot

$\endgroup$
5
  • $\begingroup$ A bit of a follow-up question... $\endgroup$
    – Ian
    Commented Mar 21, 2023 at 12:59
  • $\begingroup$ ...what exactly does the optional argument MaxPlotPoints do when used with ListContourPlot? The documentation says "ListContourPlot normally uses all of the points in the dataset". In which case, I'm not sure why increasing the value from the default "All" would make the difference it does. $\endgroup$
    – Ian
    Commented Mar 21, 2023 at 13:04
  • $\begingroup$ ...is it a case of "decreasing" the value of MaxPlotPoints rather than "increasing" it? $\endgroup$
    – Ian
    Commented Mar 21, 2023 at 13:05
  • $\begingroup$ OK, I think I've figured it out. Looks like ListContourPlot resamples the data on a square grid with the number of grid points being defined by MaxPlotPoints. $\endgroup$
    – Ian
    Commented Mar 21, 2023 at 13:22
  • $\begingroup$ @Ian, please also see my update for another possible method. $\endgroup$
    – Domen
    Commented Mar 21, 2023 at 16:24
3
$\begingroup$

The resolution of the "ListContourPlot" is too small. Add e.g.: "MaxPlotPoints->30"

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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