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 use ListPlot in order to plot four sets of data and also insert legends in my plot. Here is the Mathematica code

data1 = {{0, 48}, {50, 59}, {100, 69}, {150, 78}, {200, 86}, {250, 
87}, {300, 88}, {350, 88.5}, {400, 89}, {450, 89.5}, {500, 90}};
data2 = {{0, 50}, {21, 57}, {45, 65}, {69, 73}, {100, 82}, {150, 
86}, {200, 90}, {250, 90.5}, {300, 91}, {350, 92}, {400, 
93}, {450, 93.5}, {500, 94}};
data3 = {{0, 55}, {50, 60}, {100, 65}, {150, 72}, {200, 80}, {250, 
84}, {300, 87}, {350, 89}, {400, 91}, {450, 91.5}, {500, 92}};
data4 = {{0, 63}, {50, 66}, {100, 69}, {150, 72}, {200, 74.5}, {250, 
77}, {300, 79}, {350, 81}, {400, 83}, {450, 85}, {500, 87}};

col = {Blue, Darker[Green], Red, Magenta};
leg = {"\!\(\*SubscriptBox[\(L\), \(z\)]\) = 5", 
"\!\(\*SubscriptBox[\(L\), \(z\)]\) = 20", 
"\!\(\*SubscriptBox[\(L\), \(z\)]\) = 40", 
"\!\(\*SubscriptBox[\(L\), \(z\)]\) = 60"};
plrange = {45, 100};

P0 = ListPlot[{data1, data2, data3, data4}, Frame -> True, 
PlotStyle -> Table[{Thickness[0.003], col[[i]]}, {i, 1, 4}], 
Joined -> True, PlotMarkers -> {Automatic, 12}, 
FrameLabel -> {Subscript["M", "n"], 
"Percentage of chaotic orbits (%)"}, 
FrameStyle -> Directive[FontSize -> 17, FontFamily -> "Helvetica"], 
PlotRange -> plrange, Axes -> False, ImageSize -> 600, 
PlotLegends -> 
LineLegend[leg, LegendMarkers -> Automatic, 
LegendMarkerSize -> {{20, 25}}, 
LabelStyle -> 
 Directive[FontSize -> 15, FontFamily -> "Helvetica"]]]

which produces the following output using version 9

enter image description here

My main issue is how to increase the size of the plot markers in the legend. Inside the main plot, I can control the size of the markers by PlotMarkers -> {Automatic, 12}. On the other hand, even though I tried a lot, I cannot do the same regarding the markers in the legend. Any ideas?


Update 2015-07-22: In versions 10.x, the above input does create legend markers that are the same size as in the plot itself. But if we specify custom plot makers, we will quickly discover that the legend markers will be inconsistently sized.

ListPlot[
 Table[Accumulate@RandomReal[1, 10] + i, {i, 2}],
 PlotMarkers -> {
   Graphics[{Disk[]}, ImageSize -> 7], 
   Graphics[{Rectangle[]}, ImageSize -> 13]
 },
 Joined -> True,
 PlotLegends -> {"line1", "line2"}
 ]

Mathematica graphics

So the fundamental issue remains.

share|improve this question
    
You can try defining your own LegendMarkers. –  b.gatessucks May 6 '13 at 6:46
    
You mean of course inside the PlotLegends. How could I do this? Any clue? –  Vaggelis_Z May 6 '13 at 6:56

3 Answers 3

up vote 16 down vote accepted

You also aren't getting the right line thickness in your legend. I'd suggest a slightly different route, of creating custom legend markers, similar to the method described in this answer, and including them in a SwatchLegend.

legmarkers =  MapThread[Graphics[{#1, AbsoluteThickness[2], 
     Line[{{-1, 0}, {1, 0}}], #2}] &, {col, {Disk[{0, 0}, 0.3], 
    Rectangle[{-0.3, -0.3}, {0.3, 0.3}], 
    GeometricTransformation[
     Polygon[{{-0.3, -0.3}, {0.3, -0.3}, {0.3, 0.3}, {-0.3, 0.3}}], 
     RotationTransform[45 Degree]], 
    Polygon[0.3 ({{1, -Sqrt[3.]/2}, {0, 
         Sqrt[3]/2}, {-1, -Sqrt[3.]/2}})]} }]

enter image description here

You can then specify the markers as shown. Notice that I had to tweak the LegendMarkerSize option to get the aspect ratio of the graphics correct.

P0 = ListPlot[{data1, data2, data3, data4}, Frame -> True, 
  PlotStyle -> Table[{Thickness[0.003], col[[i]]}, {i, 1, 4}], 
  Joined -> True, PlotMarkers -> {Automatic, 12}, 
  FrameLabel -> {Subscript["M", "n"], 
    "Percentage of chaotic orbits (%)"}, 
  FrameStyle -> Directive[FontSize -> 17, FontFamily -> "Helvetica"], 
  PlotRange -> plrange, Axes -> False, ImageSize -> 600, 
  PlotLegends -> 
   SwatchLegend[leg, LegendMarkers -> legmarkers , 
    LegendMarkerSize -> {{30, 12}}, 
    LabelStyle -> 
     Directive[FontSize -> 15, FontFamily -> "Helvetica"]]]

enter image description here

share|improve this answer

In 10.x, the simplest thing is to specify the PlotMarkers directly, but as noted, the legends ignore ImageSize if specified within the graphic. The correct way to do this is to use the

{{g1, s1}, {g2, s2} ...}

form of PlotMarkers, e.g.

ListPlot[Table[Accumulate@RandomReal[1, 10] + i, {i, 2}], 
 PlotMarkers -> {{Graphics[{Disk[]}], 1/4}, {Graphics[{Rectangle[]}], 1/8}}, 
 Joined -> True, PlotLegends -> {"line1", "line2"}]

enter image description here

The reason is the legend is not otherwise passed any sizing info, e.g.

In[25]:= Options[g[[2,1]], LegendMarkers]

Out[25]= {LegendMarkers -> 
 {{Graphics[{Disk[]}, ImageSize -> 7],Automatic}, 
  {Graphics[{Rectangle[]}, ImageSize -> 13], Automatic}}}

where g is your version, and note, I swapped GraphicsBox for Graphics for readability. As these are essentially put together using Inset, the graphic is assumed to take the entire picture, and then it is scaled down to size. But, the scaling is now set to Automatic.

share|improve this answer
    
Thank you! A few questions: 1 Do you know why the legend injects EdgeForm[Directive[Opacity[0.3], Black]] in the Graphics expression of the marker? The plot only injects the colour, but not EdgeForm. I need to put a manual EdgeForm to get rid of this. 2 "I swapped GraphicsBox for Graphics for readability" <- What are you referring to? I didn't see GraphicsBox. 3 Is there a way to specify marker sizes in absolute units and still synchronize the legend and the legend with the plot? ImageSize was absolute, this way seems relative to the plot size. –  Szabolcs Jul 22 at 17:20
    
@Szabolcs 1 I think to improve readability. :) 2 copying the legend markers introduce the box form, I suspect. So, the statement is likely erroneous. 3 According to the PlotMarkers doc, its a relative size. I do not understand why it is not the same for the text form markers, e.g. "\[FilledCircle]". It's even an example. –  rcollyer Jul 22 at 17:35
2  
The problem with relative sizes is that I often need to prepare figures to size, e.g. cm = 72/2.54; ListPlot[..., ImageSize -> 9 cm]. This will give me a result that is precisely 9 cm wide when exported to PDF (well, if I also use legends, I need to set the printing style environment to "screen" to preserve dimensions during Export ...). Since here the plot marker sizes are relative, using ImageSize will again destroy the correspondence between the legend and the plot ... –  Szabolcs Jul 22 at 17:40
    
Preparing figures to size is often better than resizing them after exporting, because I can specify the font sizes in points to match the main text. Resized figures usually end up having random font sizes ... –  Szabolcs Jul 22 at 17:41
    
I see. That is problematic. Let me poke around and see. –  rcollyer Jul 22 at 18:12

For version 10.x the answer can be found in the documentation:

ListPlot[Table[Accumulate@RandomReal[1, 10] + i, {i, 2}], 
 PlotMarkers -> {{"\[FilledCircle]", 20}, {"\[FilledSquare]", 10}}, 
 Joined -> True, PlotLegends -> {"line1", "line2"}]

Mathematica graphics

This should work for graphics objects like the OP used too.

share|improve this answer
1  
This works even without explicit plot marker sizes, but this uses plot markers that are font glyphs, with all the problems that brings, such as incorrect centering. In your example all the circles are lower than they should be, relative to the line. That's why I used Graphics-based plot markers. But then the size is suddenly relative to the image size, so specifying the ImageSize option destroys the correspondence between the plot and the legend. –  Szabolcs Jul 28 at 7:22
    
Yes, I have seen that and it was one reason to share a picture because I find it ugly as hell. Additionally, you see the plot markers cover everying issue that you found out yourself. Unpolished feature. –  halirutan Jul 30 at 0:06

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.