2
$\begingroup$

I'm sure this is very easy, but I'm currently unable to make it to work properly. I just need to give different colors and style to the three curves shown in this manipulate box:

fun[gamma_] := Plot[x^2 - gamma x^3,{x, 0, 1},PlotRange -> All]

Manipulate[
    Show[
    {fun[gamma], fun[0],  fun[2]},
    PlotRange -> {{0, 1}, {-1, 1}},
    PlotStyle -> {{Thick, RGBColor[0.40, 0.40, 0.80]}, {Medium, RGBColor[0.40, 0.40, 0.40], Dashed}},
    PlotRangePadding -> None],
    {{gamma, 0.5, "gamma"}, 0, 2, 0.01}
]

Currently, the three curves are having the same color and style. I need the two fixed curves to get a medium thickness, dashed gray color, while the moving curve stays in blue. What's wrong with this simple code? Of course, I could introduce a style parameter into the fun function:

fun[gamma_, style_] := Plot[x^2 - gamma x^3, {x, 0, 1}, PlotRange -> All, PlotStyle -> style]

but is there a simpler way to achieve the same effect?

$\endgroup$
4
  • 1
    $\begingroup$ Clear[fun]; fun[gamma_] := x^2 - gamma x^3; Manipulate[ Plot[{fun[gamma], fun[0], fun[2]}, {x, 0, 1}, PlotRange -> {{0, 1}, {-1, 1}}, PlotStyle -> {{Thick, RGBColor[0.40, 0.40, 0.80]}, {Medium, RGBColor[0.40, 0.40, 0.40], Dashed}}, PlotRangePadding -> None], {{gamma, 0.5, "gamma"}, 0, 2, 0.01}] $\endgroup$
    – cvgmt
    Commented Aug 7, 2023 at 0:16
  • $\begingroup$ Hmm, I need to have separate Plotfor my full code to work... $\endgroup$
    – Cham
    Commented Aug 7, 2023 at 0:40
  • $\begingroup$ @cvgmt, you mean like the modification I made to the funfunction (last part in my question)? $\endgroup$
    – Cham
    Commented Aug 7, 2023 at 0:45
  • 1
    $\begingroup$ Plot several curves and set different PlotStyle we only need one Plot. $\endgroup$
    – cvgmt
    Commented Aug 7, 2023 at 0:52

1 Answer 1

1
$\begingroup$
directives = {Directive[Thick, RGBColor[0.40, 0.40, 0.80]], 
   Directive[Thickness@Medium, Gray, Dashed], 
   Directive[Thickness@Medium, Gray, Dashed]};

If you do not want to make any changes to fun:

1. Epilog + Show

Extract the line primitives from fun[0] and fun[2] and used them Epilog with desired styles:

Manipulate[
 Show[fun[gamma], 
  Epilog -> 
    Thread[{Rest@directives, Cases[#, _Line, All] & /@ {fun[0], fun[2]}}],
  PlotRange -> {{0, 1}, {-1, 1}}, PlotRangePadding -> None],
 {{gamma, 0.5, "gamma"}, 0, 2, 0.01}]

enter image description here

2. Post-processing

ClearAll[replaceStyles]
replaceStyles[dirs_] := Module[{d = dirs}, 
  ReplaceAll[{_Directive, l_Line} :> {Last[d = RotateLeft[d]], l}]]

You can use replaceStyles in two ways:

i. Wrap Show with replaceStyles[directives]:

Manipulate[replaceStyles[directives]@
  Show[{fun[gamma], fun[0], fun[2]}, 
    PlotRange -> {{0, 1}, {-1, 1}}, PlotRangePadding -> None], 
 {{gamma, 0.5, "gamma"}, 0, 2, 0.01}]

enter image description here

ii. Wrap {fun[...],..} in first argument of Show with replaceStyles[directives]:

Manipulate[
  Show[replaceStyles[directives] @ {fun[gamma], fun[0], fun[2]}, 
    PlotRange -> {{0, 1}, {-1, 1}}, PlotRangePadding -> None], 
 {{gamma, 0.5, "gamma"}, 0, 2, 0.01}]

enter image description here

iii. Use it as option setting for DisplayFunction in Show[...]:

Manipulate[Show[{fun[gamma], fun[0], fun[2]}, 
  DisplayFunction -> replaceStyles[directives], 
  PlotRange -> {{0, 1}, {-1, 1}}, PlotRangePadding -> None],
 {{gamma, 0.5, "gamma"}, 0, 2, 0.01}]

enter image description here

3. An alternative post-processing

ClearAll[injectDirectives]
injectDirectives[d_] := MapThread[ReplaceAll[l_Line :> {#, l}]@#2 &]@{d, #} &

Manipulate[Show[
  injectDirectives[directives]@{fun[gamma], fun[0], fun[2]},
  PlotRange -> {{0, 1}, {-1, 1}}, PlotRangePadding -> None],
 {{gamma, 0.5, "gamma"}, 0, 2, 0.01}]

enter image description here

4. A more flexible fun

Alternatively, you can modify fun to take options:

ClearAll[morefun]

morefun[gamma_, o : OptionsPattern[]] := Plot[x^2 - gamma x^3, {x, 0, 1},
 o, PlotStyle -> Directive[Thickness@Medium, Gray, Dashed]]

Manipulate[Show[
  {morefun[gamma, PlotStyle -> Automatic], morefun[0], morefun[2]},
  PlotRange -> {{0, 1}, {-1, 1}},
  PlotRangePadding -> None],
 {{gamma, 0.5, "gamma"}, 0, 2, 0.01}]

enter image description here

If you like you can add additional options:

Manipulate[
 Show[{morefun[gamma, PlotStyle -> Automatic, Mesh -> 5, 
    MeshStyle -> PointSize @ Large], 
   morefun[0], 
   morefun[2, PlotStyle -> {Dashing[Medium], AbsoluteThickness[3]}, 
      ColorFunction -> "Rainbow"]}, 
  PlotRange -> {{0, 1}, {-1, 1}}, 
  PlotRangePadding -> None], 
{{gamma, 0.5, "gamma"}, 0, 2, 0.01}]

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.