How do I label different curves in Mathematica?

500 picture 500 · Aug 28, 2011 · Viewed 35.4k times · Source

How can I label each of these lines separately :

Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}]

enter image description here

Answer

Simon picture Simon · Aug 28, 2011

There's some nice code that allows you to do this dynamically in an answer to How to annotate multiple datasets in ListPlots.

There's also a LabelPlot command defined in the Technical Note Labeling Curves in Plots

Of course, if you don't have too many images to make, then it's not hard to manually add the labels in using Epilog, for example

fns[x_] := {5 + 2 x, 6 + x}; 
len := Length[fns[x]];

Plot[Evaluate[fns[x]], {x, 0, 10}, 
 Epilog -> Table[Inset[
    Framed[DisplayForm[fns[x][[i]]], RoundingRadius -> 5], 
    {5, fns[5][[i]]}, Background -> White], {i, len}]]

outputa

In fact, you can do something similar with Locators that allows you to move the labels wherever you want:

DynamicModule[{pos = Table[{1, fns[1][[i]]}, {i, len}]},
 LocatorPane[Dynamic[pos], Plot[Evaluate[fns[x]], {x, 0, 10}],
  Appearance -> Table[Framed[Text@TraditionalForm[fns[x][[i]]], 
                  RoundingRadius -> 5, Background -> White], {i, len}]]]

In the above I made the locators take the form of the labels, although it is also possible to keep an Epilog like that above and have invisible locators that control the positions. The locators could also be constrained (using the 2nd argument of Dynamic) to the appropriate curves... but that's not really necessary.

As an example of the above code with the functions with the labels moved by hand:

fns[x_] := {Log[x], Exp[x], Sin[x], Cos[x]};

four functions