111
$\begingroup$

This example comes from the Mathematica documentation for Plot under Basic Examples.

Can someone please explain why these are each plotted as a different color in this case:

Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis]

enter image description here

But when Evaluate[] is removed, all of them are the same color:

Plot[Table[BesselJ[n, x], {n, 4}], {x, 0, 10}, Filling -> Axis]

enter image description here

I know it must have to do with the order of things being evaluated, but I'm really not sure why it is working like this - can someone please point me in the correct direction?

$\endgroup$
2
  • 3
    $\begingroup$ Hmm... I'm almost positive I've seen this question asked here before, but I can't seem to find it... $\endgroup$
    – David Z
    Commented Feb 14, 2012 at 7:26
  • 4
    $\begingroup$ Yeah, this is very much an FAQ. $\endgroup$ Commented Feb 14, 2012 at 7:55

4 Answers 4

86
$\begingroup$

The list structure is not manifest to Plot as it has the attribute HoldAll (to get a function's attributes, either use Attributes[func] or ??func). Hence Plot evaluates the Table functions as one unit and it appears as if there is only one function, not four.

Evaluate will make the list structure manifest and each function will be plotted with a separate style.

$\endgroup$
3
  • 8
    $\begingroup$ I apologize if this is naive question, but what does "The list structure is not manifest to Plot" mean? $\endgroup$
    – brown.2179
    Commented Dec 30, 2013 at 12:23
  • 2
    $\begingroup$ It means that Plot isn't seeing the argument as a list. A list is treated as one argument not many arguments as Plot has HoldAll attribute . $\endgroup$
    – Matariki
    Commented Dec 31, 2013 at 22:06
  • 1
    $\begingroup$ if I use Evaluate[...] or Evaluated->True it takes forever to plot the functions, as compared without those. Is there any way to still assign different colors without losing the speed? $\endgroup$ Commented Feb 24, 2017 at 17:40
47
$\begingroup$

Mathematica 10 update

It seems that now Plot specifically recognizes Sequence which invalidates the example given below, but not its premise. If one uses a different head that behaves similarly the behavior is still exhibited:

Plot[{1, ## &[2, 3], 4}, {x, 0, 1}, PlotRange -> {0, 5}, PlotStyle -> Thick]

Or:

f[x__] := x

Plot[{1, f[2, 3], 4}, {x, 0, 1}, PlotRange -> {0, 5}, PlotStyle -> Thick]

Or:

Plot[{1, {2, 3} /. {x__} :> x, 4}, {x, 0, 1}, PlotRange -> {0, 5}, PlotStyle -> Thick]

Analysis

Plot builds style lists based on the apparent structure of the first argument it is given, before evaluation. List is recognized and elements are styled individually, while generic functions like Table are styled as a whole.

You can see this behavior here, where Sequence acts as a "generic head":

Plot[
  {1, Sequence[2, 3], 4}, {x, 0, 1},
  PlotRange -> {0, 5},
  PlotStyle -> Thick
]

Mathematica graphics

On the other hand sub-lists are recognized and styled:

Plot[
  {1, {2, 3}, 4}, {x, 0, 1},
  PlotRange -> {0, 5},
  PlotStyle -> Thick
]

Mathematica graphics

You can read my own question about this and Sasha's excellent answer (starting from EDIT 2).


Recommended form for evaluation

I recommend that you use the option Evaluated -> True rather than Evaluate as it (still) localizes the plot variable. See:

$\endgroup$
13
  • 13
    $\begingroup$ Clever trick to use Sequence[] to constrain a group to have the same color... :) $\endgroup$ Commented Feb 14, 2012 at 8:40
  • 1
    $\begingroup$ This is not working anymore in V10 :( I liked it, this however, works: 72929. They not how to handle Sequence but ## &[2, 3] will do! :) $\endgroup$
    – Kuba
    Commented Jan 31, 2015 at 7:18
  • $\begingroup$ @Kuba Thanks for the note; I hadn't yet noticed it. I'll update the answer. $\endgroup$
    – Mr.Wizard
    Commented Jan 31, 2015 at 7:28
  • 1
    $\begingroup$ To which V10 version does your Mathematica 10 update apply? With MMA V10.1 and Plot[{1, ## &[2, 3], 4}, {x, 0, 1}, PlotRange -> {0, 5}, PlotStyle -> Thick]' I get an error (Identity::argx: Identity called with 2 arguments; 1 argument is expected` $\endgroup$
    – Sigis K
    Commented Apr 9, 2015 at 13:26
  • 1
    $\begingroup$ +1 for Evaluated -> True. Just solved my problem of long computation due to algebraic simplification of Plot's argument prior to each evaluation. $\endgroup$
    – Janosh
    Commented Jun 7, 2017 at 15:10
24
$\begingroup$

Evaluate evaluates your expression that you want to plot. This transforms the expression Table[BesselJ[n, x], {n, 4}] that you give as an argument into a list of several functions. If Plot sees several functions, it knows it can use more colors. Without Evaluate, Plot does not know you have several functions and uses only one color. That comes from the fact that Plot has the attribute HoldAll: it does not evaluate your arguments. Without evaluation the first time it sees, that several numbers are coming out of your function is during the plotting. Then it's too late to color it differently, and you get only a single colored plot.

$\endgroup$
12
$\begingroup$

Just to strengthen the idea that Plot does not evaluate the expression even with simplest operations

Plot[0 + {Sin[x], Cos[x]}, {x, 0, 2 π}, Filling -> Axis]

enter image description here

$\endgroup$
2
  • $\begingroup$ It took me 5 minutes to understand what you meant. It would be easier if you mentioned the the curves have the same color because of unevaluated expression $\endgroup$
    – emnha
    Commented Nov 26, 2019 at 4:04
  • 1
    $\begingroup$ One can check that (in v 13, at least) Plot[Evaluate[0 + {Sin[x], Cos[x]}], {x, 0, 2 π}, Filling -> Axis] works, while Plot[0 + {Sin[x], Cos[x]}, {x, 0, 2 π}, Filling -> Axis,Evaluate->True] only half works. $\endgroup$
    – Russ Lyons
    Commented Oct 12, 2023 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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