I have a code in which I read data from a file and perform a number of fits. I wish to plot these fits on a single plot and add a legend which labels each line with a value which is saved in a list (of the same length as the number of fits). However, PlotLegends
is only labelling one of the lines. As a simple example, if I run
leg = {1, 2};
Plot[{x, x^2}, {x, 0, 2}, PlotLegends -> leg]
it produces
The legends are applied correctly. However, if I define the list of functions beforehand, it doesn't work. If I now run
fs = {x, x^2};
Plot[fs, {x, 0, 2}, PlotLegends -> leg]
I get
There is no issue if I do a ListPlot
however:
lfs = Table[Table[{x, x^j}, {x, 0, 2, 0.1}], {j, 1, 2}];
ListPlot[lfs, PlotLegends -> leg]
I suspect I am failing to understand how Mathematica interprets symbols such as x
when defined the way I have defined here. For the purpose of my application, I am starting with discrete data points such as defined in lfs
above, fitting curves, and then plotting:
fits = {Fit[lfs[[1]], {1, x}, x], Fit[lfs[[2]], {1, x, x^2}, x]};
Plot[fits, {x, 0, 2}, PlotLegends -> leg]
But again I have the same issue as before.