7
$\begingroup$

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?!

$\endgroup$
9
$\begingroup$

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]

enter image description here

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]

enter image description here

Original answer:

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

enter image description here

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}

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Unbelievable Mar 16 at 20:05
  • $\begingroup$ Wonderful and intelligent!!! Fantastic $\endgroup$ – Unbelievable Mar 17 at 7:22
  • $\begingroup$ Congratulation... $\endgroup$ – Unbelievable Mar 17 at 7:23
  • $\begingroup$ Thank you @Unbelievable for the kind words and a great question. $\endgroup$ – kglr Mar 17 at 9:07
2
$\begingroup$

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: enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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