0
$\begingroup$

I am trying to predefine the functions I want to plot. A minimal example of what I am trying to achieve looks like this:

fToPlot = {x, 2 x};
legendToPlot = {"x", "2x"};
Plot[fToPlot, {x, 0, 5}, PlotLegends -> legendToPlot]

To my suprise this produces the following output:

enter image description here

which omits the second entry of the legendToPlot list completely. Why does this happen and how do I fix it?

$\endgroup$
1

1 Answer 1

4
$\begingroup$

Plot interprets fToPlot as a single item rather than a list. Thus you must Evaluate it:

fToPlot = {x, 2 x};
legendToPlot = {"x", "2x"};
Plot[Evaluate@fToPlot, 
     {x, 0, 5}, 
     PlotLegends -> legendToPlot]

Note that:

fToPlot = {x, 2 x};
Plot[Evaluate@fToPlot, 
    {x, 0, 5}, 
    PlotLegends -> "Expressions"]

also works.

$\endgroup$
5
  • $\begingroup$ The option Evaluated ->True can also be used here, I think. $\endgroup$ Commented May 9, 2016 at 23:55
  • $\begingroup$ @J.M. Nope. Doesn't work. Didn't you try it? $\endgroup$ Commented May 9, 2016 at 23:59
  • $\begingroup$ Interesting, what version are you using? Plot[fToPlot, {x, 0, 5}, Evaluated -> True, PlotLegends -> legendToPlot]works on 10.4.1 here. EDIT: Huh, the legends get messed up. This sure looks like a bug... $\endgroup$ Commented May 10, 2016 at 0:04
  • $\begingroup$ 10.4.0.0 on a Mac. I'm not sure your option Evaluated is appropriate here. $\endgroup$ Commented May 10, 2016 at 0:06
  • $\begingroup$ @J.M. - Since at least version 7.0.1, Evaluated is not a documented option for Plot or Graphics from which Plot inherits many of its options. Consequently, none of its behavior can properly be a bug. $\endgroup$
    – Bob Hanlon
    Commented May 10, 2016 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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