I'm a newbie who tries to be a good boy, and use Map instead of writing out a list of functions.

I have a table I want to Map onto:

ratios = Table[10^(n/10), {n, 0, 10}]  

and a function

rp[x_, r_] := 1000 x (r + 1)/(r + x)

which I want to Plot for the 11 ratios, for x in [0, 1]:

Plot[Map[rp, ratios], {x, 0, 1}]

This doesn't work, and I can guess why: rp requires two arguments, and MMA probably doesn't know which one is the ratio. And x also doesn't appear as a parameter. How do I fix this?


You can create a pure function that maps only on ratio as follows:

Plot[Map[rp[x, #] &, ratios], {x, 0, 1}, Evaluated -> True]

enter image description here

You will need the Evaluated -> True option in order for Plot to view the functions as several different ones and plot them in different colours.

You can also bypass having to use Map by creating your function with the Listable attribute. For example:

rp2[x_] := Function[{r}, 1000 x (r + 1)/(r + x), Listable]
Plot[rp2[x][ratios], {x, 0, 1}, Evaluated -> True]

This returns the same output as above, but automatically maps (threads) over lists.

  • $\begingroup$ The Evaluated->True works fine for the PlotStyle option but doesn't work for the PlotLegend option, any clue why? Or has any other trick has to be used? $\endgroup$ – Öskå Jul 22 '13 at 10:32
  • $\begingroup$ @Öskå Do you have an example? This works: Plot[Table[Sin[k x], {k, 1, 3}], {x, 0, 2 π}, PlotLegends -> Automatic, Evaluated -> True] $\endgroup$ – rm -rf Jul 22 '13 at 15:02
  • $\begingroup$ Using Mathematica 8 PlotLegend -> {"sine", "sine", "sine"}] only shows "sine" one time: imgur.com/1uyciA9 $\endgroup$ – Öskå Jul 22 '13 at 15:45
  • $\begingroup$ @Öskå Ugh... that's using the old PlotLegends package, which is hideous. I have no idea how to make it fix and I'm not going to touch that with a 10 ft pole :( Perhaps you might be able to use this answer, which is a far better alternative for Mathematica 8. $\endgroup$ – rm -rf Jul 22 '13 at 17:12
  • $\begingroup$ mhm thanks :-) I'll take a look at that long answer tomorow. Once again, thanks :-) $\endgroup$ – Öskå Jul 22 '13 at 17:14

This is a direct fulfilling of your attempts :

Plot[ rp[x, #] & /@ ratios, {x, 0, 1}]

enter image description here

rp[x, #] & denotes a function depending on the second argument in rp, while /@ is a shorthand for Map, i.e rp[x, #] & /@ ratios means Map[ rp[ x,#] &, ratios ].

Here is another way to plot your functions without Map :

Plot[ Evaluate[ Table[ rp[x, a], {a, ratios}]], {x, 0, 10}]

Evaluate serves here for plotting curves in various colors.

You can plot graphs of the functions as a family of curves in three dimensions using ParametricPlot3D.

ParametricPlot3D[ Evaluate[ Table[{x, a, rp[x, a]}, {a, ratios}]],
                  {x, 0, 10},  BoxRatios -> {10, 10, 5}]

enter image description here

  • $\begingroup$ @stevenvh You've got another way of doing your task in my answer without Map. $\endgroup$ – Artes Jul 10 '12 at 15:34
  • $\begingroup$ When using Evaluate in this fashion you need to guard against a global value of x. Add something about this to your answer and you'll have my vote. $\endgroup$ – Mr.Wizard Jul 10 '12 at 16:46
  • 1
    $\begingroup$ @Mr.Wizard Do you like Block[{x}, ParametricPlot3D[ Evaluate[Table[{x, a, rp[x, a]}, {a, ratios}]], {x, 0, 10}, BoxRatios -> {10, 10, 5}] ] more ? Otherwise, I'd choose Evaluated->True in ParametricPlot3D. $\endgroup$ – Artes Jul 10 '12 at 22:28
  • $\begingroup$ Block, Evaluated -> True, or simply the mention of the issue would do. You have my vote now anyway. $\endgroup$ – Mr.Wizard Jul 11 '12 at 0:15

Some things to think about:

rp[x_, r_] := 1000 x (r + 1)/(r + x)

rp[x, #] & /@ ratios

Outer[rp, {x}, ratios][[1]]

Table[rp[x, i], {i, ratios}]

  rp[x, ratios] // Thread

And Formal Symbols (looks better in the Notebook):

rp[r_] := 1000 \[FormalX] (r + 1)/(r + \[FormalX])

Plot[rp /@ ratios, {\[FormalX], 0, 1}, Evaluated -> True]

Mathematica graphics

And this (see Parameterized function and Currying in Mathematica):

rp[x_][r_] := 1000 x (r + 1)/(r + x)

Plot[rp[x] /@ ratios, {x, 0, 1}, Evaluated -> True]

Mathematica graphics

Be sure to read this and this for an explanation of Evaluated -> True.

  • 1
    $\begingroup$ +1 Very nice: Plot[Outer[rp, {x}, ratios], {x, 0, 1}] $\endgroup$ – Chris Degnen Jul 11 '12 at 10:48

When using Evaluated->True the plot legends to not display or display only one item. The answer by Artes of using the Evaluate function on the first argument to Plot will also fix the issue with plot legends, so their answer is the best from that perspective.

Plot[Evaluate[Map[rp[x, #] &, ratios]], {x, 0, 1}, 
 PlotLegends -> Automatic]

This produces

enter image description here

  • 1
    $\begingroup$ Welcome to Mma.SE! This looks like an extended comment on the accepted answer; so make sure to reference it (e.g. "Fixing the PlotLegends issue in rm -rf's answer, ... " so this answer can stand on its own. $\endgroup$ – lirtosiast Jun 30 '19 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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