Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have already obtained the plot that I want but am having trouble legending the graph correctly.

plot = ListLogLogPlot[Sort[depth4], PlotRange -> {{1, 50000}, {1, 50000}}, Joined -> True, 
PlotStyle -> {Purple}, BaseStyle -> {FontSize -> 14}];

line = LogLogPlot[11024 x^(-0.94232), {x, 1, 100000}, 
   PlotStyle -> {Orange, Dashed, Thick}];
line2 = LogLogPlot[31862 x^(-1.07076), {x, 1, 100000}, 
   PlotStyle -> {Green, Dotted, Thick}];

Show[plot, line, line2]

If I insert a legend command into the first plot, it doesn't work. I need to be able to legend my 3 curves in the graph. Thanks for your help!

share|improve this question
    
Clearly the main objective here is to have a single legend for several different plots. A related problem, addressed below by F'x, is to make a legend for a single plot with several lines or data sets. That issue is also addressed in this question: mathematica.stackexchange.com/questions/4025/… –  Jens Apr 9 '12 at 5:40
add comment

5 Answers

up vote 13 down vote accepted

When Show is used the legends use ShowLegend. Here is a demo:

depth4 = Range[20]^3;
plot = ListLogLogPlot[Sort[depth4], PlotRange -> {{1, 50000},
     {1, 50000}}, Joined -> True, PlotStyle -> {Purple},
   BaseStyle -> {FontSize -> 14}];
line = LogLogPlot[11024 x^(-0.94232), {x, 1, 100000},
   PlotStyle -> {Orange, Dashed, Thick}];
line2 = LogLogPlot[31862 x^(-1.07076), {x, 1, 100000},
   PlotStyle -> {Green, Dotted, Thick}];

Needs["PlotLegends`"]
ShowLegend[Show[plot, line, line2, ImageSize -> 500],
 {{{Graphics[{Purple, Line[{{0, 0}, {2, 0}}]}], "   depth4"},
   {Graphics[{Orange, Dashed, Line[{{0, 0}, {2, 0}}]}], "   line"},
   {Graphics[{Green, Dashed, Line[{{0, 0}, {2, 0}}]}], "   line2"}},
  LegendPosition -> {0.7, 0.2}, LegendSize -> {0.45, 0.4},
  LegendShadow -> False}]

enter image description here

share|improve this answer
    
great thank you! –  Julia Mar 26 '12 at 11:34
add comment

While in many ways Mathematica's graphics is miles ahead of its competition, the supplied legend package looks like a hack that does not belong. In its own documentation, under Inset, you will find examples of better looking legends. Building on those examples, I have a simple legend function.

ClearAll[simpleLegend]
simpleLegend[legendItems__,pos_]:=Module[{legendLine,offset,legend},
  offset=Module[{s,o,insetpts=10},
    s=pos/.{Left->0,Right->1,Bottom->0,Top->1};
    o=insetpts pos/.{Left->1,Right->-1,Bottom->1,Top->-1};
    Offset[o,Scaled[s]]
  ];
  legendLine[{lbl_,lineStyle_}]:={
    Graphics[{lineStyle,Line[{{0,0.5},{1,0.5}}]},ImageSize->{20,10},AspectRatio->0.5],
    Style[lbl,FontFamily->"Tahoma",FontSize->11,TextAlignment->Left,LineBreakWithin->False]
  };
  legend=GraphicsGrid[legendLine/@legendItems,Alignment->Left];
  Graphics@Inset[legend,offset,pos]
];

which can be used like this

labels={"data","eq1","eq2"};
styles={Directive[Purple],Directive[Orange,Dashed,Thick],Directive[Green,Dotted,Thick]};

depth4=Table[{x,(11024 x^(-0.94232))*RandomReal[{0.4,1.6}]}/.x->10^n,{n,0,5,0.1}];

plot=ListLogLogPlot[Sort[depth4],
  PlotRange->{{1,50000},{1,50000}},Joined->True,PlotStyle->styles[[1]],
    BaseStyle->{FontSize->14}
];

line=LogLogPlot[11024 x^(-0.94232),{x,1,100000},PlotStyle->styles[[2]]];
line2=LogLogPlot[31862 x^(-1.07076),{x,1,100000},PlotStyle->styles[[3]]];

Show[plot,line,line2,simpleLegend[Thread@{labels,styles},{Right,Top}]]

enter image description here

share|improve this answer
    
Thanks for this solution. How do I add instead of a dashed line, traingles or circle markers? –  Santi Feb 26 at 11:46
add comment

You can also use Epilog->Inset[...] as follows:

For

  depth4 = Range[20]^3; 
  plot =  ListLogLogPlot[Sort[depth4], PlotRange -> {{1, 50000}, {1, 50000}}, 
  Joined -> True, PlotStyle -> {Purple}, BaseStyle -> {FontSize -> 14}]; 
  line = LogLogPlot[11024 x^(-0.94232), {x, 1, 100000}, 
  PlotStyle -> {Orange, Dashed, Thick}]; 
  line2 = LogLogPlot[31862 x^(-1.07076), {x, 1, 100000}, 
  PlotStyle -> {Green, Dotted, Thick}];

as in Chris's answer,

 Show[plot, line, line2, ImageSize -> 500, 
 Epilog -> 
 Inset[Panel[
 Grid[{{Graphics[{Purple, 
 Line[{{0, 0}, {2, 0}}]}, AspectRatio -> .1, ImageSize -> 30], 
   Style["depth4", FontFamily -> "SketchFlowPrint", Bold, FontColor -> Purple]}, 
  {Graphics[{Orange, Dashed, 
     Line[{{0, 0}, {2, 0}}]}, AspectRatio -> .1, ImageSize -> 30],
    Style["line", FontFamily -> "SketchFlowPrint", Bold, FontColor -> Orange]}, 
  {Graphics[{Green, Dotted, 
     Line[{{0, 0}, {2, 0}}]}, AspectRatio -> .1, ImageSize -> 30],
    Style["line2", FontFamily -> "SketchFlowPrint", Bold, FontColor -> Green]}}, 
 Alignment -> {{Left, Left}, {Left, Left}}], ImageSize -> Full], 
 Offset[{-2, -2}, Scaled[{1, 1}]], {Right, Top}]]

gives

enter image description here

share|improve this answer
add comment

You can extract Line directives (color, thickness, dashing, etc. specifications) from the plot itself. This means that you don't have to call for a second time for the style specifications explicitly. For complex graphics (with e.g. plot markers) it might not be this simple though.

depth4 = Range[20]^3;
plot = ListLogLogPlot[Sort[depth4], 
   PlotRange -> {{1, 50000}, {1, 50000}}, Joined -> True, 
   PlotStyle -> {Purple}, BaseStyle -> {FontSize -> 14}];
line = LogLogPlot[11024 x^(-0.94232), {x, 1, 100000}, 
   PlotStyle -> {Orange, Dashed, Thick}];
line2 = LogLogPlot[31862 x^(-1.07076), {x, 1, 100000}, 
   PlotStyle -> {Green, Dotted, Thick}];

data = Show[plot, line, line2];
styles = Cases[data, {dir__, l_Line} :> {dir}, \[Infinity]]
{{Hue[0.67, 0.6, 0.6], RGBColor[0.5, 0, 0.5]}, {Hue[0.67, 0.6, 0.6], 
  RGBColor[1, 0.5, 0], Dashing[{Small, Small}], 
  Thickness[Large]}, {Hue[0.67, 0.6, 0.6], RGBColor[0, 1, 0], 
  Dashing[{0, Small}], Thickness[Large]}}
Row@{
  Show[plot, line, line2, ImageSize -> 300],
  Grid[Table[{Graphics[Append[styles[[i]], Line[{{0, 0}, {1, 0}}]], 
      ImageSize -> 50, AspectRatio -> 1/5], "plot" <> ToString@i}, {i,
      3}], Spacings -> {1, 2}]
  }

Mathematica graphics

share|improve this answer
    
You don't need InputForm for this. The Graphics expression is directly manipulable. –  Mr.Wizard Mar 27 '12 at 5:36
1  
@Mr.Wizard you're right, thanks for pointing it out. I guess this was just some bad habit surviving from pre-6 times. –  István Zachar Mar 28 '12 at 18:19
add comment

I don't know of a direct way to do that once the individual graphs have been plotted and legended. You can, however, use ListLogLogPlot on a table containing values from your functions, so as to have a single call to a Plot function, which can thus take a PlotLegend option:

Needs["PlotLegends`"];
ListLogLogPlot[{
  Sort[depth4],
  Table[{x, 11024 x^(-0.94232)} /. x -> 10^n, {n, 0, 5, 0.1}],
  Table[{x, 31862 x^(-1.07076)} /. x -> 10^n, {n, 0, 5, 0.1}]
  },
 PlotRange -> {{1, 50000}, {1, 50000}},
 Joined -> True,
 PlotStyle -> {Purple, Directive[Orange, Dashed, Thick], 
   Directive[Green, Dotted, Thick]},
 BaseStyle -> {FontSize -> 14},
 PlotLegend -> {"data", "eq1", "eq2"}]

resulting plot


PS: you were missing the definitio for depth4, so I took:

depth4 = Table[{x, (11024 x^(-0.94232))*RandomReal[{0.4, 1.6}]} /. x -> 10^n, {n, 0, 5, 0.1}];
share|improve this answer
    
Thanks a lot, however Chris Degnen's method is easier for me as i have to do this with many graphs. :) –  Julia Mar 26 '12 at 11:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.