ReplaceAll and PlotStyle and PlotLegends in multiple curves

Why do PlotStyle and PlotLegends take only a single style for all the curves if ReplaceAll rule is outside the bracket?

Plot[{α x, x^2 α} /. {α -> 2.5}, {x, -2, 6},   PlotRange -> {{-2, 6}, {-1, 6}},   PlotStyle -> {{Dotted, Blue, Thick}, {Dashed, Red}}, Frame -> True,   PlotLegends -> {"V[ϕ]", "ϵ[ϕ]"} ]

Versus

Plot[{α x /. {α -> 2.5}, x^2 α /. {α -> 2.5}}, {x, -2, 6},   PlotRange -> {{-2, 6}, {-1, 6}},   PlotStyle -> {{Dotted, Blue, Thick}, {Dashed, Red}}, Frame -> True,   PlotLegends -> {"V[ϕ]", "ϵ[ϕ]"} ]

The behavior is same even for ParametricPlot and for more than two curves.

Replay

Plot has Hold attributes, like all the other plotting functions. This means that sometimes it does not not recognize the fact that its argument is a list if the head of that argument is not explicitly List. See: Plot draws list of curves in same color when not using Evaluate.

In your first case, the head of the expression in the first argument of Plot is ReplaceAll before evaluation, so Plot sets up a single style for it:

HoldForm[{α x, x^2 α} /. {α -> 2.5}] // FullForm

(* Out:
HoldForm[ReplaceAll[
List[Times[α, x], Times[Power[x, 2], α]],
List[Rule[α, 2.5]]
]
]
*)

In your second case, the head of the expression is List, so Plot recognizes that it needs to use more than one style:

HoldForm[{α x /. {α -> 2.5}, x^2 α /. {α -> 2.5}}] // FullForm

(* Out:
HoldForm[
List[
ReplaceAll[Times[α, x], List[Rule[α, 2.5]]],
ReplaceAll[Times[Power[x, 2], α], List[Rule[α, 2.5]]]
]
]
*)

As usual, the solution is to explicitly Evaluate the first argument:

Plot[Evaluate[{α x, x^2 α} /. {α -> 2.5}], {x, -2, 6},
PlotRange -> {{-2, 6}, {-1, 6}},
PlotStyle -> {{Dotted, Blue, Thick}, {Dashed, Red}},
Frame -> True, PlotLegends -> {"V[ϕ]", "ϵ[ϕ]"}
]

Category: graphics Time: 2016-07-29 Views: 1
Tags: graphics