# Forcing a particular altitude in a contour plot to have a specific color

How can a particular altitude in a contour plot be forced to have a particular color, despite the applied color function? (Specific to my case, the altitude of 0).

• do you happen to have a MWE? that will make it easier to answer. May 27, 2020 at 9:44
• The easiest way would be to add a constant, but this might depend on what you are doing. May 27, 2020 at 9:45

Here is an approach that post-processes the contours:

ClearAll[formatContour]
formatContour[value_, format__: Red] :=
HoldPattern[Tooltip[{Directive[color_, directives__], graphics__}, value]] :>
Tooltip[{Directive[format, directives], graphics}, value]


I constructed this after inspection of the output of InputForm of a ContourPlot. We can then apply it as follows:

cp = ContourPlot[Sin[x y], {x, -Pi/2, Pi/2}, {y, -Pi/2, Pi/2}];

cp /. formatContour[0]


... or you can specify directives:

cp /. formatContour[0.2, Blue, Thickness[0.02]]


### 1. Exclusions + ExclusionsStyle

Row[ContourPlot[Sin[x y], {x, -Pi/2, Pi/2}, {y, -Pi/2, Pi/2},
ImageSize -> Medium,
Exclusions -> {Sin[x y] == #},
ExclusionsStyle -> {Red, Red}] & /@ {0, .4}]


### 2. MeshFunctions + Mesh + MeshStyle

Row[ContourPlot[Sin[x y], {x, -Pi/2, Pi/2}, {y, -Pi/2, Pi/2},
ImageSize -> Medium, PlotPoints -> 100,
MeshFunctions -> {Sin[# #2] &},
Mesh -> {{#}},
MeshStyle -> Directive[Purple, Thick]] & /@ {0., .4}]


### 3. ContourStyle

minmax = #[{Sin[x y], -Pi/2 <= x <= Pi/2 && -Pi/2 <= y <= Pi/2}, {x, y}] & /@
{NMinValue, NMaxValue}

{-1., 1.}

Row[ContourPlot[Sin[x y], {x, -Pi/2, Pi/2}, {y, -Pi/2, Pi/2},
ImageSize -> Medium,
ContourStyle -> (Replace[N@Rest@FindDivisions[minmax, 10],
{# -> Directive[Magenta, AbsoluteThickness[3], Dashed], _ -> Automatic}, {1}])]&/@
{0., .4}]


### 4. Contours

Row[ContourPlot[Sin[x y], {x, -Pi/2, Pi/2}, {y, -Pi/2, Pi/2},
ImageSize -> Medium,
Contours -> ((N@FindDivisions[minmax, 10] /.
# -> {#, Directive[Thick, Green]}))] & /@ {0., .4}]