4
$\begingroup$

Is there a way to tell Mathematica to use plain lines for contour lines associated with positive level values and dashed otherwise ?

Can't seem to find anything in the help documentation.

Thanks for the help.

$\endgroup$
2

4 Answers 4

8
$\begingroup$

Maybe this? :

ContourPlot[Evaluate@((x*y == #) & /@ Range[-8, 8, 1]), {x, -3, 3}, {y, -3, 3}, 
ContourStyle -> (If[# >= 0, {Directive[Red]}, {Directive[Blue, Dashed]}] & /@ 
Range[-8, 8, 1])]

enter image description here

$\endgroup$
5
$\begingroup$
f[x_, y_] = x*y;

Show[
 ContourPlot[f[x, y],
  {x, -3, 3}, {y, -3, 3},
  Contours -> Range[1, 8],
  ContourShading -> None,
  ContourStyle -> Red],
 ContourPlot[f[x, y],
  {x, -3, 3}, {y, -3, 3},
  Contours -> Range[-8, -1],
  ContourShading -> None,
  ContourStyle -> Directive[Blue, Dashed]],
 ContourPlot[f[x, y],
  {x, -3, 3}, {y, -3, 3},
  Contours -> {0},
  ContourShading -> None,
  ContourStyle -> Directive[Lighter[Gray], AbsoluteThickness[1]]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ I must be missing something. Why would this answer be downvoted? Bob's answer also anticipates a zero contour. $\endgroup$
    – JimB
    Jan 29, 2018 at 23:12
1
$\begingroup$

This hack-ish method relies on ContourPlot[] generating contours with the $z$-value being stored in a Tooltip[]:

ContourPlot[x y, {x, -3, 3}, {y, -3, 3}, Contours -> 12, ContourShading -> None,
            ContourStyle -> {}] /. Tooltip[prims_, val_] :> 
                                   Tooltip[Prepend[prims,
                                                   Switch[Sign[val],
                                                          -1, Directive[Blue, Dashed],
                                                          0, Opacity[1/2, Gray],
                                                          1, Red]], val]

differently-colored contours

$\endgroup$
0
$\begingroup$

Yes, you can create two separate plots (one for positive, one for negative), and combine them:

Show[{ContourPlot[{Abs[Sin[x] Sin[y]] == 0.5, 
    Abs[Cos[x] Cos[y]] == 0.5}, {x, -3, 3}, {y, -3, 0}, 
   ContourStyle -> Dashed], 
  ContourPlot[{Abs[Sin[x] Sin[y]] == 0.5, 
    Abs[Cos[x] Cos[y]] == 0.5}, {x, -3, 3}, {y, 0, 3}]}, 
 PlotRange -> All]

image

$\endgroup$

Your Answer

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

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