# Mixing dashed and plain contours in a contourplot

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.

• Posible duplicate: https://mathematica.stackexchange.com Commented Jan 29, 2018 at 23:43
• That is right on point. Thanks. Commented Jan 30, 2018 at 9:26

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


f[x_, y_] = x*y;

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


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

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]


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]