4
$\begingroup$

Given a (possibly disjoint) region, defined by a discrete set of points, how can I use ListContourPlot[] together with Mesh to highlight a specific area of the plot? For instance, how can I mesh the region where the points are smaller than a certain value?

Here I construct a minimal example where I try to highlight the area where the values of a discrete sample of the function $f(x) = e^{x^2 - y^2}$ are smaller then one.

data = Table[Exp[x^2 - y^2], {x, -1, 1, .01}, {y, -1, 1, .01}];

ListContourPlot[
 data
 , Contours -> {1.0}
 , ContourStyle -> Transparent
 , Mesh -> 25
 , MeshFunctions -> {#1 + #2 &}
 , MeshStyle -> Thick
 ]

I also tried using MeshFunctions -> {Piecewise[{{#1 + #2 &, #3 <= 1 &}, {None, #3 > 0 &}}]}, but I had no luck.

I am aware that this can be done for symbolic functions through RegionPlot[], however I am not sure how to extend this to numerical data.

Mesh in ListContourPlot

$\endgroup$
4
  • 4
    $\begingroup$ MeshFunctions -> {( # + #2) Boole[#3 <= 1] &}? $\endgroup$
    – kglr
    Commented Mar 21, 2022 at 23:21
  • $\begingroup$ @kglr this works perfectly, thank you! $\endgroup$ Commented Mar 21, 2022 at 23:55
  • 1
    $\begingroup$ data = Table[Exp[x^2 - y^2], {x, -1, 1, .01}, {y, -1, 1, .01}]; ListContourPlot[data, Contours -> {1.0}, ContourStyle -> Transparent, Mesh -> 25, MeshFunctions -> {Function[{x, y, f}, If[f > 1, x + y, 0]], Function[{x, y, f}, If[f < 1, x - y, 0]]}, MeshStyle -> {Red, Directive[Thick, Green]}] $\endgroup$
    – cvgmt
    Commented Mar 22, 2022 at 1:17
  • $\begingroup$ @cvgmt this also works perfectly. Thank you everyone for the excellent replies! $\endgroup$ Commented Mar 22, 2022 at 2:22

3 Answers 3

5
$\begingroup$
data = Table[Exp[x^2 - y^2], {x, -1, 1, .01}, {y, -1, 1, .01}];

ListContourPlot[data, Contours -> {1.0}, ContourStyle -> Transparent, 
 ContourShading -> {Directive[Orange, HatchFilling[-Pi/4, 1, 10]], 
   Directive[Cyan, HatchFilling[Pi/4, 1, 8]]}]

enter image description here

$\endgroup$
4
$\begingroup$
data = Table[Exp[x^2 - y^2], {x, -1, 1, .01}, {y, -1, 1, .01}];

Show[
 ListContourPlot[data,
    Contours -> {1},
    Mesh -> 25,
    MeshFunctions -> {#1 + #2 &},
    MeshStyle -> Directive[Thick, #[[1]]],
    RegionFunction -> #[[2]]] & /@
  {{Red, 
    Function[{x, y, f}, f < 1]},
   {Black, Function[{x, y, f}, f > 1]}}]

enter image description here

$\endgroup$
3
$\begingroup$

Excellent replies as usual by @cvgmt and @Bob Hanlon, but I'd like to give it a go using Piecewise

With

data = Table[Exp[x^2 - y^2], {x, -1, 1, .01}, {y, -1, 1, .01}];

The following works nicely I think

ListContourPlot[data,
 Contours -> {1.0},
 ContourStyle -> Transparent,
 Mesh -> 25,
 MeshFunctions -> {Piecewise[{{#1 + #2, #3 <= 1}}] &, 
   Piecewise[{{None, #1 + #2 >= 1}}] &},
 MeshStyle -> Thick]

piecewise

$\endgroup$

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.