Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

plot - How do I label different curves in Mathematica?

How can I label each of these lines separately :

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...