# ContourPlot with certain contour values omitted

Suppose I want contour plot of $x^2-y^2$, for $x,y\in[0,1]$ (my actual function is more complicated). Over this interval, $x^2-y^2$ is sometimes positive and sometimes negative. Using ContourPlot I get all the contours, negative as well as positive.

But what if I wanted only those contours with values $\geq 0$ to show, while those contours whose value is less than zero are not displayed or (worst case) shown by the same color (say white). I doubt if this is possible at all, but any help is much appreciated.

• ContourPlot[ ConditionalExpression[x^2 - y^2, x^2 >= y^2], {x, 0, 1}, {y, 0, 1}] or ContourPlot[x^2 - y^2, {x, 0, 1}, {y, 0, 1}, RegionFunction -> (#^2 >= #2^2 &)]?
– kglr
Nov 23, 2017 at 11:01
• @kglr Awesome. Why not post it as an answer?
– Deep
Nov 23, 2017 at 11:06

You can use the option RegionFunction:

ContourPlot[x^2 - y^2, {x, 0, 1}, {y, 0, 1}, RegionFunction -> (#^2 >= #2^2 &)] Or use ConditionalExpression

ContourPlot[ConditionalExpression[x^2 - y^2, x^2 >= y^2], {x, 0, 1}, {y, 0, 1},
Contours -> 4] • does not this plot restricting the domain of the function? I understand that the OP ask for showing the contours that indicate levels $f>0$ while plotting the function over the whole domain... Nov 23, 2017 at 19:32
• @JoséAntonioDíazNavas, i agree with you. That's why i posted this as a comment and posted as answer per OP's suggestion.
– kglr
Nov 23, 2017 at 23:56

Maybe this:

ContourPlot[x^2 - y^2, {x, 0, 1}, {y, 0, 1},Contours -> Rescale@Range[#]] & /@ {5, 10, 20} or:

ContourPlot[x^2 - y^2, {x, 0, 1}, {y, 0, 1}, ColorFunction -> (Blend[{White, Blue}, #] &),
Contours -> Rescale@Range[#]] & /@ {5, 10, 20} Even the contours can be customised, of course. In this case continuous for $f\geq 0$ and Dashed for $f<0$:

Show[{ContourPlot[x^2 - y^2, {x, 0, 1}, {y, 0, 1}, ContourStyle -> None],
ContourPlot[Evaluate@((x^2 - y^2 == #) & /@ Range[-1, 1, 0.1]), {x, 0, 1}, {y,
0, 1}, ContourStyle -> (If[#>=0, {Directive[Black]}, {Directive[Black, Dashed]}] & /@
Range[-1, 1, 0.1])]}] 