Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Difference in Plot when using Evaluate vs when not using Evaluate

Assume one has vector of functions (e.g., vec={x,x^2}).

The command Plot[vec, {x, -2, 2}, PlotStyle -> {Blue, Red}] allows one to plot curves in different colors.

enter image description here

Any use of the replacement operator, however, causes all curves to adopt the final listed color. For example, Plot[vec /. x -> y, {y, -2, 2}, PlotStyle -> {Blue, Red}] gives

enter image description here

How does one specify different colors for different curves if a replacement (/.) is used within the plot command?

share|improve this question

marked as duplicate by Mr.Wizard Mar 21 '12 at 0:02

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.

up vote 9 down vote accepted

Plot has attribute HoldAll (you can check with Attributes[Plot]), which makes it not recognize the two separate curves and "sees" it as one. Use Evaluate to overcome this.

Plot[Evaluate[vec /. x -> y], {y, -2, 2}, PlotStyle -> {Blue, Red}]
share|improve this answer
Perfect, thank you. I didn't know about the HoldAll attribute. Having now read about it, if I understand correctly, Hold[x/.x->y] prevents the substitution from taking place. How does Plot manage to plot the curves (incorrect colors aside) over the domain $y\in\left[-2,2\right]$ if the substitution $x\mapsto y $ never takes place? – user001 Mar 20 '12 at 19:56
There's probably some ReleaseHold called further down...though that's a good question :) – tkott Mar 20 '12 at 19:57
@user001 HoldAll does not prevent the replacement from taking place — you can see this with simply doing HoldAll[vec /. x -> y]. However, what it does do is prevent Plot from recognizing that there are two functions in that list and makes it see only one. You can learn more from this answer by Sasha to a related question. – R. M. Mar 20 '12 at 20:15

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