Legends for Plot's (solution)
- To: mathgroup at smc.vnet.net
 - Subject: [mg102309] Legends for Plot's (solution)
 - From: dr DanW <dmaxwarren at gmail.com>
 - Date: Wed, 5 Aug 2009 05:43:02 -0400 (EDT)
 
The PlotLegends` package is badly dated and creates unattractive
legends.  In Mathematica 7, there is support for very attractive
legends, but only for the new Chart types.  The mechanism is a little
different than in PlotLegends.  Legends are implemented by placing the
wrapper Legended[] around the data being plotted.  The chart function
knows to ignore Legended[] except when looking for legend text.
On the other hand, Plot[] knows nothing about Legended[], and will not
recognize anything in that wrapper as data.  Plot[] does recognize the
Tooltip[] wrapper, however.
Below is code that will create an attractive legend out of any
Tooltip'ed curve in a Plot.  This will only work on lines, not
markers, so if you use it on ListPlot, make sure Joined->True.  The
main function is LineLegendFromTooltips[].
Usage example:
LineLegendFromTooltips[ Plot[{Tooltip[x,"x"], Tooltip[x^2,"x^2"]},{x,
0,3} ] ]
-------code------------
LineStylesFromTooltips[plot_Graphics] :=
    Cases[plot, Tooltip[{s__, l_Line}, tt_] :>
        Grid[{{Graphics[Flatten[{s, Line[{{0, 0}, {1, 0}}]}],
                ImageSize -> {24, 8}, AspectRatio -> 8/24,
        ImagePadding -> 0],
              tt}}], Infinity]
LineLegendFromTooltips[plot_Graphics] :=
    Labeled[plot, Style[Column[LineStylesFromTooltips[plot]], "TR",
        ShowStringCharacters -> False], Right]
-----end code-------
I sincerely hope this function will be unnecessary in version 8.0.
Daniel