0

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?

1 Answers1

4

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.

David G. Stork
  • 41,180
  • 3
  • 34
  • 96