Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

listplot and legend (again)

340 views
Skip to first unread message

Arkadiu...@gmail.com

unread,
Aug 14, 2007, 7:37:38 AM8/14/07
to
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


Stern

unread,
Aug 15, 2007, 4:30:58 AM8/15/07
to
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

thomas

unread,
Aug 21, 2007, 5:16:20 AM8/21/07
to
On Aug 14, 1:37 pm, Arkadiusz.Ma...@gmail.com wrote:
> Hi,
>
> I know that there has been at least one post on this topic but I still
> can't drawlegendinside ListPlot.

>
> When i plot several curves, colors for them are chosen automaticaly
> by Mathematica. How can I plot alegendfor them?

>
> May I ask for an example, please? Why quite good solution existed in
> MultipleListPlot in Mathv5.2 just vanished????
>
> Arek

Hello, Arek,

When Mathematica 6 assigns plotcolors automatically in ListPlot (or
ListLinePlot) it uses the colors defined in ColorData[1]. Therefore,
the first set of data will be plotted in color ColorData[1,1], the
second in colorData[1,2] and so on. (I don't think that that is
mentioned anywhere in the Help system)

Once you know this, it is quite straigtforeward to make a legend, even
without the use of the PlotLegend package.

Here are two examples. Example 1 prints the legend as part of the
image (as Epilog), Ex. 2 prints it as separate element

data = Table[RandomReal[], {4}, {5}];

legend = Framed@
Column[Table[
Style["data " <> ToString[i],ColorData[1, i], Bold, "Graphics",
15], {i, Length[data]}]];

(* Example 1 *)
ListLinePlot[data, Epilog -> Inset[legend, {.5, .5}],
ImageSize -> 300]

(* Example 2 *)
Row[{ListLinePlot[data, ImageSize -> 300], legend}]

Hope that helps,
Thomas


Muench, Thomas

unread,
Aug 22, 2007, 4:48:57 AM8/22/07
to
Hello, Arek

When Mathematica automatically chooses colors for plotting in ListPLot
or ListLinePlot, it takes colors defined in ColorData[1], in the order
defined there. That means, your first line (or set of points) is plotted
in ColorData[1,1], the second in ColorData[1,2], and so on. I don't
think that this is mentioned anywhere in the Help system.

Once you know this, it is straight foreward to make your own legends,
with not need for the PlotLegend package.

Here are two examples; the first one plots the legend as part of the
Plot itself (as Epilog), the second shows the legend as a separate
element next to the Plot.

data = Table[RandomReal[], {4}, {5}];

legend = Framed@
Column[Table[

Style["data " <> ToString[i], ColorData[1, i], Bold, "Graphics",


15], {i, Length[data]}]];

(* Example 1 *)
ListLinePlot[data, ImageSize -> 300,
Epilog -> Inset[legend, {.5, .5}]]

(* Example 2 *)
Row[{ListLinePlot[data, ImageSize -> 300], legend}]

Hope that helps,
Thomas


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?

0 new messages