tags:

views:

80

answers:

3

Is it possible that when I Plot a function in Mathematica, it will automatically put near it it's equation(i.e. y = 2x) or even some other text?

At first glance I don't find any option for it, but if there is one I'd like to know.

Thanks

+2  A: 

"Near its equation" is the problem. This isn't an easy problem to solve, and it becomes somewhat impossible when you start getting "busy" graphs with overlapping plots and so on.

I don't have a good example to show, but usually I'll define a "labelling function" that takes the same input as the function being plotted, which places a dot on the graph and writes some text nearby. This has the advantage of being able to easily vary the location of the text but still have it tied to the function.

Will Robertson
+3  A: 

One way to do this, which automatically associates the expression with the style used to plot it, is to use the PlotLegends standard add-on package. The output doesn't look very good by default; I recommend setting the LegendShadow -> None option and using Style on the expressions you stick in the legend to make them look better. Also, loading the package inflicts some funny redefinitions on Plot and related functions which can break some other things if you're not careful.

Pillsy
Ugh, is PlotLegends any less ugly in Mma 7? I tried it once and ever since have drawn my legends by hand.
Will Robertson
It's still pretty bad, but the newer (>=6.0) styling directives give you more room to fix it.
Pillsy
+2  A: 

Using Mathematica 6 or higher, I often use Tooltip to help me identify plot curves:

Plot[Tooltip[Sin[x]], {x, 0, 8 Pi}]

Alas, this is only useful when using the graph interactively since you must hover the mouse cursor over the curve. It doesn't work so well on paper or on a static image.

You can use the Epilog option to manually place some text on the plot, as in this example:

Plot[
  Sin[x], {x, 0, 8 Pi},
  Epilog -> Text["My Text", Offset[{32, 0}, {14, Sin[14]}]]
]

Tweak the arguments of Offset to taste.

This works if you do not mind manual placement. Automatic placement poses some challenges, depending upon the kinds of functions that you wish to plot. But if you know something of the general characteristics of the functions of interest, you could write a function that calculates nice looking values for the Offset arguments. For example, if I knew I was going to plot lots of exponential decline functions, I might define something like the function myPlot in this example:

SetAttributes[myPlot, HoldAll]
myPlot[function_, {var_, min_, max_}] :=
  Plot[
    function, {var, min, max},
    Epilog -> Text[function, Offset[{40, 0}, {var, function} /. var -> min + (max - min)/20]],
    PlotRange -> All, AxesOrigin -> {0, 0}
  ]

... where the arguments to Offset are computed automatically using some arbitrary constants that work reasonably well for these kinds of plots:

Manipulate[
  myPlot[1000 E^(-d t), {t, 0, 100}, "My Label"],
  {d, 0.01, .2}
]

Since all of these options are programmable, the sky's limit as to how much sophistication you could code up for the label placement. Of course, such programming drifts farther and farther away from the ideal of a built-in option to Plot that just magically drops on some text next to the function. Mathematica 8 or 9 maybe :)

WReach