How to define the line style in advance of plot function

Wrap style1,style2 and style3 with Evaluate:

p1 = Plot[Sin[π*x], {x, 0, 1}, Evaluate@style1];
p2 = Plot[Sin[2 π*x], {x, 0, 1}, Evaluate@style2];
p3 = Plot[Sin[3 π*x], {x, 0, 1}, Evaluate@style3];

Show[p1, p2, p3, PlotRange -> All]

enter image description here

Alternatively, force evaluation by passing style1,style2 and style3 as arguments:

p1 = Plot[Sin[π*x], {x, 0, 1}, #]& @ style1;
p2 = Plot[Sin[2 π*x], {x, 0, 1}, #]& @ style2;
p3 = Plot[Sin[3 π*x], {x, 0, 1}, #]& @ style3;
Show[p1, p2, p3, PlotRange -> All]

same picture

You can also inject the styles using With:

p1 = With[{style1=style1},Plot[Sin[π*x], {x, 0, 1},style1]];
p2 =  With[{style2=style2},Plot[Sin[2 π*x], {x, 0, 1},style2]];
p3 = With[{style3=style3}, Plot[Sin[3 π*x], {x, 0, 1},style3]];
Show[p1, p2, p3, PlotRange -> All]

same picture


The most straightforward way of general style definition in MMA 12 is:

st=Sequence["color","thickness", "any-of-other-style-definitions"];
Plot[f[x],{x,x0,xl},st, PlotRange->All]

The Sequence produces the true sequence of definitions which are not wrapped by any additional figure brackets { }. Thus, one can use it with any following options like PlotRange or something else without Evaluate.

Tags:

Plotting