# Red-green dashing instead of red-white dashing

We can simply use

ContourPlot[{Abs[Sin[x] Sin[y]] == 0.5,
Abs[Cos[x] Cos[y]] == 0.5}, {x, -3, 3}, {y, -3, 3},
ContourStyle -> {{Black, Thickness[0.004]},
Directive[Red, AbsoluteDashing[{2, 3}]]}]


to have a plot in which one curve is dashed and another is solid. The dashed curve is colored by red and white. The color of white determines the dashing property. But how to have a curve of course a dashed curve with red and green color. I mean how green can be substituted with white for implement of dashing?!

Update: Define a function to change the style for dashed primitives to two-colored dashing:

ClearAll[directive, twoColorDashing]
directive /: {directive[dirs___, dashing : (AbsoluteDashing | Dashing)[{__}],
cols_: {Red, Green}], l__Line} := {Directive[dirs], cols[[1]],
Dashing[{}], l, cols[[2]], CapForm["Butt"], dashing, l};

twoColorDashing = Module[{colors = #2}, # /.
Directive[a___, b : (_AbsoluteDashing | _Dashing)] :>
directive[a, b, First[colors = RotateRight[colors]]]] &;


Examples:

cp1 = ContourPlot[{Abs[Sin[x] Sin[y]] == 0.5,
Abs[Cos[x] Cos[y]] == 0.5}, {x, -3, 3}, {y, -3, 3},
ContourStyle -> {{Black, Thickness[0.05], Dashing[{.05, .02}]},
Directive[Opacity[1], Red, Thickness[0.03], AbsoluteDashing[{5, 3}]]}];

colors = {{Red, Yellow}, {Cyan, Purple}};

twoColorDashing[cp1, colors]


If Dashing or AbsoluteDashing does not appear as the last directive for a contour no change is made to the styling of that contour:

cp2 = ContourPlot[{Abs[Sin[x] Sin[y]] == 0.5,  Abs[Cos[x] Cos[y]] == 0.5},
{x, -3, 3}, {y, -3, 3},
ContourStyle -> {{Black, Dashing[{.05, .02}], Thickness[0.05]},
Directive[Opacity[1], Red, Thickness[0.03], AbsoluteDashing[{5, 3}]]}];

twoColorDashing[cp2, RotateRight@colors]


Another way to cheat:

ContourPlot[{Abs[Sin[x] Sin[y]] == 0.5,
Abs[Cos[x] Cos[y]] == 0.5,
Abs[Cos[x] Cos[y]] == 0.5},
{x, -3, 3}, {y, -3, 3},
ContourStyle -> {{Black, Thickness[0.004]}, Green,
Directive[Red, CapForm["Butt"], AbsoluteDashing[{5, 3}]]}]


You can also post-process ContourPlot output to inject the primitives with desired style:

cp = ContourPlot[{Abs[Sin[x] Sin[y]] == 0.5, Abs[Cos[x] Cos[y]] == 0.5},
{x, -3, 3}, {y, -3, 3},
ContourStyle -> {{Black, Thickness[0.004]},
Directive[Opacity[1], Red, Thick, AbsoluteDashing[{5, 3}]]}];

cp /. {d : Directive[__, _AbsoluteDashing], l__Line} :>
{Thick, Green, l, d, CapForm["Butt"], l}


• This trick had come to my mind as well but I wish to have a way without repeating a function twice. In any way thank you so much Mar 16, 2020 at 20:05
• Wonderful and intelligent!!! Fantastic Mar 17, 2020 at 7:22
• Congratulation... Mar 17, 2020 at 7:23
• Thank you @Unbelievable for the kind words and a great question.
– kglr
Mar 17, 2020 at 9:07

Overlapping two plots is the easiest:

cp2=ContourPlot[{Abs[Sin[x] Sin[y]]==0.5,Abs[Cos[x] Cos[y]]==0.5},{x,-3,3},{y,-3,3},ContourStyle->{{Black,Thickness[0.004]},Directive[Red,AbsoluteDashing[{2,3}]]}];
cp1=ContourPlot[Abs[Cos[x] Cos[y]]==0.5,{x,-3,3},{y,-3,3},ContourStyle->Green];
Show[{cp1,cp2}]


giving: