Different outputs with same inputs

Why if I plot

Plot[Table[a x, {a, 5}], {x, -3, 3}]

all the lines have same colour but if I separate in this way

Table[a x, {a, 5}];
Plot[%, {x, -3, 3}]

the lines have different colour?

This is because the plot function has the attribute HoldAll. Because of this, the Plot function cannot tell that the head of the first argument is a list since the Table is not evaluated and, hence, has the Head Table. Generally when plotting something that will evaluate to a list of functions you should override the HoldAll for the first argument in this way:

Plot[Evaluate[Table[a x, {a, 5}]], {x, -3, 3}]

This serves two purposes. One is the one I just mentioned. The other one–and more important one–is that the Plot will then not need to evaluate the Table for every point on the graphs. It will evaluate the Table once before plotting and then plot the separate functions. This can often lead to a very significant performance advantage, especially if the evaluate of the first argument to yield the list of functions is itself time consuming.

Thanks for your answer!!

Now the question is:

Why has the plot function the attribute HoldAll???

Thanks again :slight_smile: