Re: Bug with map in conjuntion with plotstyle?
- To: mathgroup at smc.vnet.net
 - Subject: [mg117986] Re: Bug with map in conjuntion with plotstyle?
 - From: Bill Rowe <readnews at sbcglobal.net>
 - Date: Thu, 7 Apr 2011 08:05:56 -0400 (EDT)
 
On 4/6/11 at 5:11 AM, jason.lee.quinn at gmail.com (Jason Quinn) wrote:
>values = {0.5, 1.0, 2.0};
>f[x_, y_] := x^2 + y
>Plot[{f[x, #]} & /@ values, {x, 0, 6}, PlotStyle -> {{Red}, {Blue},
>{Yellow}}]
>thing = {f[x, #]} & /@ values
>Plot[thing, {x, 0, 6}, PlotStyle -> {{Red}, {Blue}, {Yellow}}]
>I'm trying to use map to generate a list of functions to plot and
>use PlotStyle to define the color and style of the plot.
>Unfortunately, it seems like there's a bug here. When map is used
>with Plot, it applies the first plotstyle to all the plots.
>I think the above is a good test case. The first plot only plots
>red. The second plot plots red, blue, and green as desired.
>Am I misunderstanding something about plot, map, or plot style? Or
>is this a bug?
This isn't a bug. Instead, it is a direct consequence of how
Plot evaluates its arguments. With
Plot[f[x, #]} & /@ values, ...
Plot first substitutes a numerical value for x then evaluates
f[x, #]&/@values. That is when the evaluation of the argument is
complete, Plot sees a list of numerical values which is exactly
what you would get if you were to plot a single multi-value
function and only one color is used in the plot.
When you first do thing = f[x, #]&/@values then do
Plot[thing...]. Plot sees something that has head List and sees
it as a list of functions to be plotted. Consequently, each is
colored according to your PlotStyle directive.
Doing
Plot[Evaluate[f[x,#]&/@values] ...
forces evaluation of f[x,#]&/@values before Plot does anything.
So, this will result in separate colors.