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.

As the title indicates, I want to change the current position of the legends in a combined plot. The Mathematica code I used is

dataRG00 = {{2.5, 15.15}, {2.75, 13.73}, {3, 12.64}, {3.25, 
11.56}, {3.5, 10.68}, {3.75, 9.65}, {4, 9.31}, {4.25, 9.27}, {4.5,
 8.88}, {4.75, 8.21}, {5, 8.0}};
dataRG11 = {{2.5, 2.07}, {2.75, 3.0}, {3, 3.84}, {3.25, 4.58}, {3.5, 
5.56}, {3.75, 6.32}, {4, 6.73}, {4.25, 7.10}, {4.5, 7.06}, {4.75, 
7.07}, {5, 7.29}};
dataRG21 = {{2.5, 20.56}, {2.75, 19.50}, {3, 18.51}, {3.25, 
17.51}, {3.5, 16.52}, {3.75, 15.63}, {4, 14.75}, {4.25, 
13.85}, {4.5, 13.00}, {4.75, 12.26}, {5, 11.6}};
dataRG32 = {{2.5, 5.24}, {2.75, 5.14}, {3, 5.00}, {3.25, 4.51}, {3.5, 
4.25}, {3.75, 3.62}, {4, 2.56}, {4.25, 1.66}, {4.5, 1.42}, {4.75, 
1.29}, {5, 1.25}};
dataRG43 = {{2.5, 10.31}, {2.75, 10.41}, {3, 10.56}, {3.25, 
10.46}, {3.5, 10.25}, {3.75, 10.33}, {4, 10.67}, {4.25, 
10.23}, {4.5, 9.21}, {4.75, 9.60}, {5, 10.5}};
dataRG85 = {{2.5, 4.62}, {2.75, 4.64}, {3, 4.72}, {3.25, 4.67}, {3.5, 
4.46}, {3.75, 4.49}, {4, 4.37}, {4.25, 4.22}, {4.5, 4.25}, {4.75, 
4.06}, {5, 3.8}};
dataRG99 = {{2.5, 1.05}, {2.75, 0.77}, {3, 0.66}, {3.25, 0.74}, {3.5, 
0.86}, {3.75, 0.81}, {4, 0.90}, {4.25, 0.76}, {4.5, 0.90}, {4.75, 
0.90}, {5, 1.08}};
dataCH = {{2.5, 41.0}, {2.75, 42.81}, {3, 44.05}, {3.25, 45.96}, {3.5,
 47.42}, {3.75, 49.18}, {4, 50.68}, {4.25, 52.90}, {4.5, 
55.27}, {4.75, 56.60}, {5, 56.48}};

col = {Darker[Green], Red, Blue, Yellow, Orange, Cyan, Magenta, 
Darker[Gray]};
ley = {"Box", "1:1 linear", "2:1 boxlet", "3:2 boxlet", "4:3 boxlet", 
"8:5 boxlet", "Other resonances", "Chaotic"};
plrange = {{2.4, 5.1}, {-2, 60}};

legendPlot[xl_List] := 
ListPlot[xl, Frame -> True, 
PlotStyle -> Table[{Thickness[0.003], col[[i]]}, {i, 1, 8}], 
Joined -> True, PlotMarkers -> {Automatic, 15}, 
FrameLabel -> {"α", "Percentage %"}, 
FrameStyle -> Directive[FontSize -> 17, FontFamily -> "Helvetica"], 
PlotRange -> plrange, Axes -> False, ImageSize -> 600, 
Epilog -> 
 Inset[Grid[
 MapIndexed[{Graphics[{col[[First[#2]]], 
      Inset[Style[{\[FilledCircle], \[FilledSquare], \
 \[FilledDiamond], \[FilledUpTriangle], \[FilledDownTriangle], \
 \[EmptyCircle], \[EmptySquare], \[EmptyDiamond]}[[First[#2]]], 15]], 
      Thickness[0.08], Line[{{0, 0}, {1, 0}}]}, 
     AspectRatio -> 0.5, ImageSize -> 20], 
    Style[ley[[First[#2]]], Bold, FontSize -> 10, 
     FontFamily -> "Helvetica"]} &, xl]], 
Offset[{-2, -2}, Scaled[{1, 1}]], {Right, Top}]]

P0 = legendPlot[{dataRG00, dataRG11, dataRG21, dataRG32, dataRG43, 
dataRG85, dataRG99, dataCH}]

which produces the following plot

enter image description here

Obviously, the position of the legends is wrong since they overlap part of the plot. What I want, is to move the legends outside the frame of the plot and particularly at the right hand side of it and adjust it at the middle of the y range. I "played" a lot with Offset and Scaled but I without success.

Any ideas?

share|improve this question
    
Have you tried PlotLegend? –  darksky Apr 4 '13 at 9:53

2 Answers 2

up vote 3 down vote accepted
With[{xl = {dataRG00, dataRG11, dataRG21, dataRG32, dataRG43, dataRG85, dataRG99, dataCH}}, 
Row[{ListPlot[xl, Frame -> True, 
  PlotStyle -> Table[{Thickness[0.003], col[[i]]}, {i, 1, 8}], 
  Joined -> True, PlotMarkers -> {Automatic, 15}, 
   FrameLabel -> {"\[Alpha]", "Percentage %"}, 
FrameStyle -> Directive[FontSize -> 17, FontFamily -> "Helvetica"], 
PlotRange -> plrange, Axes -> False, ImageSize -> 600], 
Grid[MapIndexed[{Graphics[{col[[First[#2]]], 
     Inset[Style[{\[FilledCircle], \[FilledSquare], \
\[FilledDiamond], \[FilledUpTriangle], \[FilledDownTriangle], \
\[EmptyCircle], \[EmptySquare], \[EmptyDiamond]}[[First[#2]]], 15]], 
     Thickness[0.08], Line[{{0, 0}, {1, 0}}]}, AspectRatio -> 0.5,
     ImageSize -> 20], 
   Style[ley[[First[#2]]], Bold, FontSize -> 10, 
    FontFamily -> "Helvetica"]} &, xl]]}, Spacer[10]]]

enter image description here

If you have Version 9, you can also use PlotLegends and LineLegend:

 ListPlot[{dataRG00, dataRG11, dataRG21, dataRG32, dataRG43, dataRG85, 
 dataRG99, dataCH}, Frame -> True, 
 PlotStyle -> Table[{Thickness[0.003], col[[i]]}, {i, 1, 8}], 
 Joined -> True, PlotMarkers -> {Automatic, 15}, 
 FrameLabel -> {"\[Alpha]", "Percentage %"}, 
 FrameStyle -> Directive[FontSize -> 17, FontFamily -> "Helvetica"], 
 PlotRange -> plrange, Axes -> False, ImageSize -> 600, 
 PlotLegends -> LineLegend[ley, LegendMarkers -> Automatic]]

enter image description here

Update: "to place the legends at the upper right part of the plot" and "to increase the vertical distance between each label", you can use

 PlotLegends ->  Placed[LineLegend[ley, LegendMarkers -> Automatic, 
   LegendMarkerSize -> {{30, 25}}], {After, Top}]

to get

enter image description here

share|improve this answer
    
Thank you! One more question... what if I want to move the legends at the upper right part of the plot (always outside)? Or increase the vertical distance between each label (i.e. 1:1, 2:1) ? –  Vaggelis_Z Apr 4 '13 at 10:03
    
I do have the 9.0 version. –  Vaggelis_Z Apr 4 '13 at 10:06
    
@Vaggelis_Z, to increase the vertical distance between lables you can use the option LegendMarkerSize; e.g., LegendMarkerSize -> {{55, 25}} where the second element of the list controls the vertical size of each legend. Re placement of the legend on the upper right, I think there was a Q/A about that; I will post the link if I can find it. –  kguler Apr 4 '13 at 10:19
    
Many many thanks! –  Vaggelis_Z Apr 4 '13 at 10:41

You can do:

PlotLegends -> 
    Placed[ LineLegend[{"xx", "yy", "zz", "kk", "ww"}, 
                        Joined -> Automatic, LegendFunction -> Frame, 
                        LegendMarkerSize -> {{35, 10}}], 
           {{0.2, 0.5}, {0.2, 0.02}}]

The position is given by Placed[***, {{0.2, 0.5}, {0.2, 0.02}}]

share|improve this answer

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.