6

When I draw multiple functions like exp,2^x,3^x, is it possible to generate a label of each function?

My code now:

  Plot[{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic, PlotStyle -> {Red, Green, Blue}]

What I mean is generate 3 labels in this case to tell the user what function it is.

Such as:

enter image description here

How do you generate this?

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
sam
  • 2,049
  • 3
  • 20
  • 27
  • Although it uses Graphics[] instead of Plot, [this](http://stackoverflow.com/a/7547457/353410) may give you a few ideas – Dr. belisarius Jan 02 '12 at 03:05
  • 2
    possible duplicate of [How do I label different curves in Mathematica?](http://stackoverflow.com/questions/7221315/how-do-i-label-different-curves-in-mathematica) – abcd Jan 02 '12 at 04:15

4 Answers4

4

I am not sure what the rules are for adding another, different answer for the same question. But here is another, different way to do it. If I am supposed to add this to my first answer, I can do that.

You can add the text labels, by hand, using Text commands. I think it looks better. Here is one way:

Clear[x];
funs = {Exp[x], 2^x, 3^x};
funNames = Style[#, 12] & /@ funs;

(*the x-axis plot range used *)
from = -5; to = 2;

(* generate the coordinates at the end of the plot lines*)
pos = Map[{to, #} &, funs /. x -> to];

(*generate the text labels *)
text = Map[Text[#[[1]], #[[2]], {-1, 0}] &, Thread[{funNames, pos}]];

Plot the final result (added a little of padding to plot range so that the labels added are seen completely)

Plot[funs, {x, from, to},
 PlotRangePadding -> {1, 1},
 PlotStyle -> {Red, Green, Blue},
 PlotRange -> All,
 Epilog -> text
 ]

enter image description here

update (1)

Sam asked below for an simpler way. I am not sure now. But one way to make it easier to use this method, is to make a function and then simply call this function once to generate the Text labels. You can put this function where you put all your other functions you use all the time, and just call it.

Here is something: First write the function

 (*version 1.1*)

myLegend[funs_List,                    (*list of functions to plot*)
   x_,                                 (*the independent variable*)
   from_?(NumericQ[#] && Im[#] == 0 &),(*the x-axis starting plot range*)
   to_?(NumericQ[#] && Im[#] == 0 &)   (*the x-axis ending plot range*)
   ] := Module[{funNames, pos, text, labelOffset = -1.3},

   (*make label names*)
   funNames = Style[#, 12] & /@ funs;

   (*generated the coordinates at the end of the plot lines*)
   pos = Map[{to, #} &, funs /. x -> to];

   (*generate the Text calls*)
   text = Map[Text[#[[1]], #[[2]], {labelOffset, 0}] &, 
     Thread[{funNames, pos}]]
   ];

And now just call the above any time you want to plot with labels. It will be just 1-2 extra lines of code. like this:

Clear[x]
from = -5; to = 2;
funs = {Exp[x], 2^x, 3^x};

Plot[funs, {x, from, to}, PlotRangePadding -> {1, 1}, 
 PlotStyle -> {Red, Green, Blue}, PlotRange -> All, 
 Epilog -> myLegend[funs, x, from, to]]

enter image description here

Here are few examples:

enter image description here

You can modify it as you want.

Nasser
  • 12,849
  • 6
  • 52
  • 104
  • It looks very nice,but it's a bit too complicated for me. Is there any simple way to do it? Thank you~ – sam Jan 02 '12 at 04:05
4

Perhaps this works: Use Tooltip in Plot to generate a Graphics object with tooltips. Then rewrite the tooltip to place the desired text in the desired location:

Plot[
 Tooltip@{Exp[x], 2^x, 3^x}, {x, -5, 2},
 AspectRatio -> Automatic,
 PlotStyle -> {Red, Green, Blue},
 PlotRange -> All,
 PlotRangePadding -> 1.1] /.
{
 Tooltip[{_, color_, line_}, tip_]
 :>
 {Text[Style[tip, 14], {.25, 0} + line[[1, -1]]], color, line}
}

Mathematica graphics

Arnoud Buzing
  • 15,383
  • 3
  • 20
  • 50
  • This is a really beautiful and clever use of the power of replacements in symbolic graphics objects. We usually do all our post processing this way too. – Cetin Sert Jan 03 '12 at 20:22
3

Alternative way with Tooltip displaying labels while the mouse pointer is at the function graphs :

Plot[Tooltip@{Exp[x], 2^x, 3^x}, {x, -5, 2}, AspectRatio -> Automatic,
                                             PlotStyle -> {Red, Green, Blue}]
Artes
  • 1,133
  • 1
  • 17
  • 24
1

One way is to use PlotLegends

(I do not like it too much, but it is an easy way to do what you want)

<< PlotLegends`
Clear[x];
funs = {Exp[x], 2^x, 3^x};
legends = Map[Text@Style[#, "TR", 12] &, funs];
Plot[Evaluate@funs, {x, -5, 2}, AspectRatio -> Automatic, 
 PlotStyle -> {Red, Green, Blue}, PlotLegend -> legends]

enter image description here

see help to customize the legend more. The above uses defaults.

http://reference.wolfram.com/mathematica/PlotLegends/tutorial/PlotLegends.html

Nasser
  • 12,849
  • 6
  • 52
  • 104