# ListLinePlot with legend

I have here three example data sets which are plotted with ListLinePlot.
Each data sets get its own color and the same is assigned to the labels.

How can I Map or simplify the line:

Legended[Transpose[{x, y1}], “y1”], Legended[
Transpose[{x, y2}], “y2”], Legended[Transpose[{x, y3}], “y3”]

so that the plot below is obtained?

Example code:

SeedRandom[“x”];
x = Range[10]*0.3;

SeedRandom[“y1”];
y1 = RandomReal[{0, 1}, 10];

SeedRandom[“y2”];
y2 = RandomReal[{0, 1}, 10];

SeedRandom[“y3”];
y3 = RandomReal[{0, 1}, 10];

ListLinePlot[{Legended[Transpose[{x, y1}], “y1”],
Legended[Transpose[{x, y2}], “y2”],
Legended[Transpose[{x, y3}], “y3”]}, Joined -> True,
PlotRange -> All, BaseStyle -> {FontSize -> 20}, Frame -> True,
FrameLabel -> {{“y”, “”}, {“x”, “Title”}}, ImageSize -> 600]

=================

1

Legended[Transpose[{x, ToExpression@#}], #] & /@ {“y1”, “y2”, “y3”}?
– kglr
Aug 9 at 13:13

This one liner works perfect … Again and again I have the problem that two solutions exist for my question, both great and I can only vote for one … thank you
– mrz
Aug 9 at 14:21

=================

1

=================

I would use the option PlotLegends, e.g.

data = Transpose[{x, #}]& /@ {y1, y2, y3};

ListLinePlot[data,
PlotRange -> All, BaseStyle -> {FontSize -> 20}, Frame -> True,
FrameLabel -> {{“y”, “”}, {“x”, “Title”}}, ImageSize -> 600,
PlotLegends -> {“y1”, “y2”, “y3”}]

Legended is useful for adding legends to a subset of data sets, but PlotLegends is much more convenient. Note, I also removed Joined -> True as ListLinePlot uses that by default.

Great … thanks
– mrz
Aug 9 at 14:01

Please excuse me for a short additional question: How can I change the font size of the plot legends? E.g. When I increase to BaseStyle -> {FontSize -> 40} then the size of the legends is too small.
– mrz
Aug 9 at 14:48

This way? PlotLegends -> (Style[#, 40] & /@ {“y1”, “y2”, “y3”})]
– mrz
Aug 9 at 14:59

1

If you want to change the font size for both the plot and the legend, set LabelStyle -> {FontSize -> size} in the plots options and the legend will pick it up, too. But, if you only want to set it for the legend, or have a different value for the legend set PlotLegends -> LineLegend[labels or Automatic, LabelStyle -> {FontSize -> size}].
– rcollyer
Aug 9 at 15:02