5
$\begingroup$

In this example Plot does not color each curve differently:

Plot[{vx0 Sin[t], vy0 Cos[t]} /. {vy0 -> 1, vx0 -> 1}, {t, 0, 12}]

but in this example it does:

Plot[{vx0 Sin[t] /. {vy0 -> 1, vx0 -> 1}, 
vy0 Cos[t] /. {vy0 -> 1, vx0 -> 1}}, {t, 0, 12}]

How do I automatically enable Mathematica to give a different color to each curve?

$\endgroup$

marked as duplicate by Michael E2 plotting Jul 22 at 15:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

10
$\begingroup$

Compare the FullForm of your two inputs:

FullForm[Hold[Plot[{vx0 Sin[t], vy0 Cos[t]} /. {vy0 -> 1, vx0 -> 1}, {t, 0, 12}]]]
(* 
   Hold[Plot[ReplaceAll[List[Times[vx0,Sin[t]],Times[vy0,Cos[t]]],
   List[Rule[vy0,1],Rule[vx0,1]]],List[t,0,12]]]
*)

FullForm[Hold[Plot[{vx0 Sin[t] /. {vy0 -> 1, vx0 -> 1}, vy0 Cos[t] /.
  {vy0 -> 1, vx0 -> 1}}, {t, 0, 12}]]]
(*
    Hold[Plot[List[ReplaceAll[Times[vx0,Sin[t]],List[Rule[vy0,1],Rule[vx0,1]]],
   ReplaceAll[Times[vy0,Cos[t]],List[Rule[vy0,1],Rule[vx0,1]]]],List[t,0,12]]]
*)

For the second, the head of the first argument to Plot is List. When Plot sees this, it uses the length of the list as the number of colors to choose. For the first, the head is ReplaceAll. In this case, it doesn't anticipate that there will be more than one plot, so it only chooses one color.

Evaluate lets ReplaceAll do its job before Plot sees the argument. This yields a list, which allows plot to determine the number of colors to choose.

$\endgroup$
4
$\begingroup$

See

Note the difference if t has a value:

t = 2.;

Plot[{vx0 Sin[t], vy0 Cos[t]} /. {vy0 -> 1, vx0 -> 1}, {t, 0, 12}, 
 Evaluated -> True]

enter image description here

Plot[Evaluate[{vx0 Sin[t], vy0 Cos[t]} /. {vy0 -> 1, vx0 -> 1}], {t, 0, 12}]

enter image description here

$\endgroup$

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