Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Mathematica 6.0 - Legend

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75942] RE: [mg75861] Mathematica 6.0 - Legend
  • From: "David Annetts" <davidannetts at aapt.net.au>
  • Date: Mon, 14 May 2007 03:26:03 -0400 (EDT)
  • References: <200705120704.DAA23845@smc.vnet.net>

Hi Robert,

>   I want to create a Legend on a ListLinePlot I made.  I have 
> 4 data groups I plot on one graph and I want to identify each 
> one by its colour and a name.
> 
>   I read about the Legend Package, but it seems, if I 
> understand correctly, to work only with Plot like graphs. I 
> do not understand why it is so difficult (it seems to me 
> anyway) to add a legend to a graph. It is common practice in 
> science to identify a plot with a legend, so others can 
> understand your graphics. With the Plot command it seems 
> straight forward to do this with PlotLegend (mind you I do 
> not see how I can move the legend around...). Why this 
> package is not available with all other graphics tools, like 
> ListPlot, ListLinePlot,....
> 
>  
> 
> Or maybe it is just me ...  J

It might just be you ....:)  For 5.2 and earlier, Legends can be moved using
the directive LegendPosition->{x, y}.  For other plotting commands, Legends
can be used with the command ShowLegend.  It's tedious, but not particularly
difficult.  

I hope that Legend use with 6.0 is much less tedious.

Anyway, given
	
	data = Random[] & /@ Range[32];
	p1 = ListPlot[data];

We can define a line using

	lin = {Graphics[Line[{{-1, 0}, {0, 0}}]]};

And a legend using

	lgd = {Transpose[{lin, {"Legend"}}],
      LegendLabel -> StyleForm["Label", FontWeight -> "Bold"],
      LegendOrientation -> Vertical,
      LegendSize -> {.4, .2},
      LegendShadow -> .01,
      LegendPosition -> {-.2, -.95}
      };

We use them together with ShowLegend[] as

	ShowLegend[p1, lgd];

The tedium comes when trying to position the legend, but the two positions
below should get you started.

	LegendPosition -> {-.8, .35} 	(* top left *)
	LegendPosition -> {.5, -.5} 	(* bottom right *)

For graphs of more than one data set, you'll need muliple Legend entries,
for example, given

	data = {Random[], Random[]} & /@ Range[32];
	data = Transpose[data];
	p1 = MultipleListPlot[data, PlotStyle -> {Red, Blue}, SymbolShape ->
None];

One Legend might be

	lin = Graphics[{#, Line[{{-1, 0}, {0, 0}}]}] & /@ {Red, Blue};
	
	lgd = {Transpose[{lin, {"Curve 1", "Curve 2"}}],
      LegendLabel -> StyleForm["Comparison", FontWeight -> "Bold"],
      LegendOrientation -> Vertical,
      LegendSize -> {.4, .2},
      LegendShadow -> .01,
      LegendPosition -> {.5, -.5}
      };
	
	ShowLegend[p1, lgd];

Regards,

Dave.


  • Prev by Date: Re: Plot & warning messages (v6)
  • Next by Date: Re: Working with D, definition of a function
  • Previous by thread: Mathematica 6.0 - Legend
  • Next by thread: RE: Mathematica 6.0 - Legend