2
$\begingroup$

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]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Legended[Transpose[{x, ToExpression@#}], #] & /@ {"y1", "y2", "y3"}? $\endgroup$ – kglr Aug 9 '16 at 13:13
  • $\begingroup$ 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 $\endgroup$ – mrz Aug 9 '16 at 14:21
6
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Great ... thanks $\endgroup$ – mrz Aug 9 '16 at 14:01
  • $\begingroup$ 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. $\endgroup$ – mrz Aug 9 '16 at 14:48
  • $\begingroup$ This way? PlotLegends -> (Style[#, 40] & /@ {"y1", "y2", "y3"})] $\endgroup$ – mrz Aug 9 '16 at 14:59
  • 3
    $\begingroup$ 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}]. $\endgroup$ – rcollyer Aug 9 '16 at 15:02

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.