开发者

How do I label different curves in Mathematica?

How can I label ea开发者_Go百科ch of these lines separately :

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

How do I label different curves in Mathematica?


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}]]

How do I label different curves in Mathematica?

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]};

How do I label different curves in Mathematica?


Mathematica 9 now provides easy ways to include legends.

Plot[{{5 + 2 x}, {6 + x}}, {x, 0, 10}, PlotLegends -> "Expressions"]


You can insert legends in your plot by loading the PlotLegends package

<<PlotLegends`;
Plot[{5+2 x,6+x},{x,0,10},
    PlotLegend->{"5+2x","6+x"},LegendShadow->None,
    LegendPosition->{0.3,-0.5},LegendSpacing->-0,LegendSize->0.5]

How do I label different curves in Mathematica?


However, let me also note my dislike of this package, primarily because it's extremely counterintuitive, laden with too many options and does not provide a clean experience right out of the box like most of Mathematica's functions. You will have some fiddling around to do with the options to get what you want. However, in plots and charts where you do want a legend, this can be handy. Also see the comments to this answer and this question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜