MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: What's going on here (Table-generated lists)?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98003] Re: What's going on here (Table-generated lists)?
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Fri, 27 Mar 2009 05:38:26 -0500 (EST)
  • References: <gqfl2e$ksp$1@smc.vnet.net>

Erik Max Francis wrote:
> I'm seeing a difference in behavior with Plot in conjunction with Table, 
>   and I'm rather confused at the reason.  If I use Plot to plot a list 
> of functions, it works as expected, and I get a plot with each function 
> in its own color:
> 
> Plot[{x, 2 x, 3 x, 4 x, 5 x}, {x, -1, 1}]
> 
> If I use Table, then this doesn't work the same way -- the five lines 
> are all plotted with the same color:
> 
> Plot[Table[k x, {k, 1, 5}], {x, -1, 1}]
> 
> I verified that the both objects created have a Head of List, so I don't 
> see anything obviously different about them.
> 
> Here's what confuses me more:  If I generate the table separately and 
> then use that as a result in a later evaluation in the Plot, then I get 
> the colors back:
> 
> In[25]:= Table[k x, {k, 1, 5}]
> 
> Out[25]= {x, 2 x, 3 x, 4 x, 5 x}
> 
> In[26]:= Plot[%, {x, -1, 1}]
> 
> What is going on here?  Is it something like Plot does a special scan of 
> its first argument before Evaluating it, and if it doesn't start out as 
> a List, it concludes that it's all one thing and plots it with the same 
> color, as opposed to an explicitly provided list of separate colors?
> 

Yes, that is right.

In newer versions of Mathematica, Plot[] is a complicated function that 
tries to guess whether the argument you provided needs to be evaluated 
symbolically before substituting in numerical values for the plot 
variable, or not.  Of course it cannot always guess right.  In this case 
it doesn't evaluate the Table before starting the plotting, so it thinks 
that it'll have only one thing to plot, and thus it uses only one colour.

IMO this is quite silly.  If it is able to plot all five lines, it 
should be able to figure out that it needs five colours ...

But guessing behaviour can be manually overridden with the (I think 
undocumented) option Evaluated.

Plot[Table[k x, {k, 1, 5}], {x, -1, 1}, Evaluated -> True]

(The default is Evaluated -> Automatic)

If you don't know about this option, you can use the more traditional

Plot[Table[k x, {k, 1, 5}] // Evaluated, {x, -1, 1}]

However, this is not completely equivalent to using the option 
Evaluated:  with Evaluated -> True, x still gets localized inside Plot, 
and the plotting will work correctly even if x has a value:

x = 1.
Plot[Table[k x, {k, 1, 5}] // Evaluated, {x, -1, 1}]
Plot[Table[k x, {k, 1, 5}], {x, -1, 1}, Evaluated -> True]


  • Prev by Date: Re: HoldAll for Integrate
  • Next by Date: Does mathematica7 support fontconfig?
  • Previous by thread: Re: What's going on here (Table-generated lists)?
  • Next by thread: Re: What's going on here (Table-generated lists)?