Mathematica 9 is now available
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: listplot and legend (again)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg80231] Re: [mg80193] listplot and legend (again)
  • From: Stern <nycstern at gmail.com>
  • Date: Wed, 15 Aug 2007 04:19:22 -0400 (EDT)
  • References: <200708141113.HAA09965@smc.vnet.net>

Personally, I like the way ShowLegend works now; I often take
advantage of the flexibility. However, I agree that it might have been
nice had Wolfram provided some more sample code. You may find the
following useful.

You pass the following function two arguments -- a list of coordinate
lists, and a list of captions. The default colorlist has 15 members;
if you want to pass a larger number of coordinate lists than that,
you'll need to create and pass a longer colorlist as well. If it's not
clear how that works, let me know and I'll post more detailed
examples. The optional parameter ls can be set to change the legend's
size, while the optional parameter lp can be set to change its
position.


In[]:= Needs["PlotLegends`"]

In[]:=Options[withlegend] = {lp -> {1, .25}, ls -> Automatic,
colorlist -> ColorData[1, "ColorList"]};

In[]:= withlegend[listoflists_, listofnames_, OptionsPattern[]] :=
  Module[{graph, legend},
   graph = Show[
     Map[ListPlot[#[[1]], Mesh -> Full, MeshStyle -> #[[3]],
        Joined -> True, PlotStyle -> {PointSize[0.03], #[[3]]},
        PlotRange -> All, Frame -> False,
        DisplayFunction -> Identity] &,
      Transpose[{listoflists, listofnames,
        Take[OptionValue[colorlist], Length[listoflists]]}]]];
   legend = {(({Graphics[{#1, Thick, Line[{{0.05, 0.7}, {1, .7}}]}],
            Style[#2, FontSize -> 10]} &) @@ #1 &) /@
      Transpose[{Take[OptionValue[colorlist], Length[listoflists]],
        listofnames}], LegendPosition -> OptionValue[lp],
     LegendSize -> OptionValue[ls], LegendShadow -> {0, 0}};
   ShowLegend[graph, legend]]


Try the following to see it in action:

In[]:= list1 = {{1, .2}, {2, .4}, {3, .343}, {4.1, .3321}};

In[]:= list2 = {{1, .21}, {2, .34}, {3, .43}, {4, .1}};

In[]:= list3 = {{1, .12}, {2, .34}, {3, .56343}, {4.1, .13321}};

In[]:= withlegend[{list1, list2, list3}, {"one", "two", "three"}]


You can customize the design of the graphs and legend as much as you
like. In my "production" version of this function, it is possible to
pass most of the optional arguments one can send to ListPlot
(AxesOrigin, etc.), as well as adjusting the appearance of the legend
in many ways at runtime.

My production version takes lists of data objects as variables, rather
than lists of coordinate lists, as above. Each data object has a
header which includes the object's name, obviating the need to pass
the separate list of captions, as I have done above. Also, as I work
almost exclusively with financial data, my production version is based
around DateListPlot rather than ListPlot. These changes are minor,
however, once you are comfortable with the code.


I hope that helps.

-stern





On 8/14/07, Arkadiusz.Majka at gmail.com <Arkadiusz.Majka at gmail.com> wrote:
>  Hi,
>
>  I know that there has been at least one post on this topic but I
> still  can't draw legend inside ListPlot.
>
>  When i plot several curves, colors for them  are chosen automaticaly
> by Mathematica. How can I plot a legend for them?
>
>  May I ask for an example, please? Why quite good solution existed in
> MultipleListPlot in Mathv5.2 just vanished????
>
>  Arek
>
>
>


  • Prev by Date: Re: Complexity explosion in linear solve
  • Next by Date: Re: Re: Version 6 "Mathematica Book" - updated
  • Previous by thread: listplot and legend (again)
  • Next by thread: Re: listplot and legend (again)