# How to change the plot style of several curves, in this manipulate box?

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}},
{{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?

• 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}] Aug 7, 2023 at 0:16
• Hmm, I need to have separate Plotfor my full code to work...
– Cham
Aug 7, 2023 at 0:40
• @cvgmt, you mean like the modification I made to the funfunction (last part in my question)?
– Cham
Aug 7, 2023 at 0:45
• Plot several curves and set different PlotStyle we only need one Plot. Aug 7, 2023 at 0:52

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


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


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


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


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


### 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}},
{{gamma, 0.5, "gamma"}, 0, 2, 0.01}]


Manipulate[