Message Boards Message Boards

1
|
2157 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Using Map inside Plot to show multiple curves?

Hi.

I am attempting to plot a series of moisture saturation curves (relative humidity) versus dry-bulb temperature, and label them (e.g., 10%, 20%, etc.). There are ten lines to be plotted, so I wrap the Plot function around a Map to generate each line. When I do this only the first (RH = 10%) line gets labeled (see attached notebook). However, when I explicitly Plot the output of the Map all lines get labeled. Can anyone explain this behavior? From what I see, both expressions are equivalent (obviously not!), and should produce identical output. What am I missing?

POSTED BY: J. Leko
5 Replies

What's different if I explicitly plot the results of the Map function versus evaluating the Map inside the Plot?

Attributes@Plot
(* {HoldAll, Protected, ReadProtected} *)

The difference is caused by the HoldAll attribute. Try this

Plot[Evaluate[# saturatedMoistureContent[t] & /@ Range[0.1, 1, 0.1]], {t, 0, 50}, 
 PlotLabels -> {"10%", "20%", "30%", "40%", "50%", "60%", "70%", 
   "80%", "90%", "100%"}]

If you want all the lines to have the default color, add

PlotStyle -> ColorData[97][1]
POSTED BY: Rohit Namjoshi

The reason for this behavior is that Plot[] has the attribute HoldAll, i.e. basically its arguments stay in an unevaluated form. Consequently you can/should write (for your specific case):

Plot[Evaluate[# saturatedMoistureContent[t] & /@ Range[0.1, 1, 0.1]], 
{t, 0, 50}, PlotLabels -> {"10%", "20%", "30%", "40%", "50%", "60%", "70%", 
   "80%", "90%", "100%"}]

or likewise:

Plot @@ {# saturatedMoistureContent[t] & /@ Range[0.1, 1, 0.1], {t, 0, 50}, PlotLabels -> {"10%", "20%", "30%", "40%", "50%", "60%", 
    "70%", "80%", "90%", "100%"}}
POSTED BY: Henrik Schachner

Hi Rohit, sorry, I did not see your response in time ... Regards -- Henrik

POSTED BY: Henrik Schachner

Thanks to you both! That was exactly what I needed. :-)

Rohit, in another version of the notebook I used PlotStyle->Blue for that purpose. Thanks for thinking ahead on that.

POSTED BY: J. Leko

Hi Henrik,

No worries. I have done the same in the past. Sometimes there is a significant lag between when an answer is posted and when it appears on the site.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract