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: [mg97975] Re: [mg97956] What's going on here (Table-generated lists)?
  • From: "David Park" <djmpark at comcast.net>
  • Date: Fri, 27 Mar 2009 05:33:04 -0500 (EST)
  • References: <1959064.1238066744184.JavaMail.root@m02>

It is because Plot has the Attribute HoldAll. It sees only one argument so
it uses only one color. (But this is something of a feature!)

The following works because the first argument is evaluated before the Plot
algorithm goes into action.

Plot[Evaluate@Table[k x, {k, 1, 5}], {x, -1, 1}]

Or

data = Table[k x, {k, 1, 5}]
{x, 2 x, 3 x, 4 x, 5 x}

Plot[data, {x, -1, 1}]

But the following does not work because the Table is not evaluated first.

data := Table[k x, {k, 1, 5}]
Plot[data, {x, -1, 1}]


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  


From: Erik Max Francis [mailto:max at alcyone.com] 

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?

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
   In this world, nothing is certain but death and taxes.
    -- Benjamin Franklin




  • Prev by Date: Re: What's going on here (Table-generated lists)?
  • Next by Date: Re: How do I hide all input in a Mathematica notebook?
  • Previous by thread: Re: What's going on here (Table-generated lists)?
  • Next by thread: Re: What's going on here (Table-generated lists)?