1
$\begingroup$

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

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

2 Answers 2

4
$\begingroup$

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]

default modification

... or you can specify directives:

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

blue 0.2

$\endgroup$
2
$\begingroup$

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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