How to automatically generate a function name in math?

When I draw several functions, such as exp, 2 ^ x, 3 ^ x, can I label each function?

My code is:

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

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

For instance:

enter image description here

How do you create this?

+6
source share
4 answers

Maybe this works: use the Tooltip in Plot to create a Graphics object with tooltips. Then rewrite the tooltip to place the desired text in the right place:

 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

+4
source

I'm not sure which rules are designed to add another, different answer for the same question. But here is another, different way to do this. If I have to add this to my first answer, I can do it.

You can add text labels manually 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}]]; 

Highlight the end result (add some indentation to select the range so that the added labels are fully visible)

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

enter image description here

update (1)

Sam asked for an easier way. I'm not sure now. But one way to make it easier to use this method is to create a function and then just call that function once to create text labels. You can put this function where all your functions you use all the time, and just call it.

Here's something: write a function first

  (*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 it anytime you want to build shortcuts. This will be just 1-2 extra lines of code. eg:

 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 some examples:

enter image description here

You can change it as you wish.

+4
source

Alternative way: Tooltip display labels when the mouse pointer is on the function graphs:

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

One way is to use PlotLegends

(I don’t really like it, but this 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 in setting up the legend more. The above example uses the default values.

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

+1
source

Source: https://habr.com/ru/post/904869/


All Articles