6
$\begingroup$

I have a pretty plot with a legend

enter image description here

But I want to make the legend the same width as the graph, i.e. causing all the text to be on one line.

Code to make the legend here:

legend = SwatchLegend[{Blue, Orange, Red, Darker[LightGreen, 0.4], 
    Black}, {"Photoemission Signal", "Linear Fit", "Fit Begin", 
    "Fit End", "Baseline" }, LabelStyle -> {FontFamily -> "cmr10"}, 
   LegendMarkers -> {Graphics[{EdgeForm[Black], Opacity[0.6], 
       Disk[]}], Graphics[Line[{{0, 0}, {1, 1}}]], 
     Graphics[Line[{{0, 0}, {0, 1}}]], 
     Graphics[Line[{{0, 0}, {0, 1}}]], 
     Graphics[Line[{{0, 0}, {1, 0}}]]}];

Changing the legend margins does not have the desired effects.

$\endgroup$
  • $\begingroup$ Look at LegendMarkerSize. $\endgroup$ – rcollyer Mar 6 '17 at 14:46
  • $\begingroup$ That changes the size of the legend dots etc, which will change the size of the legend, but not change the width of the legend box. I like the sizes, but I want it to be same width. $\endgroup$ – Tomi Mar 6 '17 at 14:47
8
$\begingroup$

You can set LegendLayout:

legends = 
  SwatchLegend[{Blue, Orange, Red, Darker[LightGreen, 0.4], 
    Black}, {"Photoemission Signal", "Linear Fit", "Fit Begin", 
    "Fit End", "Baseline"}, LabelStyle -> {FontFamily -> "cmr10"}, 
   LegendMarkers -> {Graphics[{EdgeForm[Black], Opacity[0.6], 
       Disk[]}], Graphics[Line[{{0, 0}, {1, 1}}]], 
     Graphics[Line[{{0, 0}, {0, 1}}]], 
     Graphics[Line[{{0, 0}, {0, 1}}]], 
     Graphics[Line[{{0, 0}, {1, 0}}]]}, 
   LegendLayout -> (Row[Flatten[#], Spacer[2]] &)];

ListPlot[RandomInteger[10, 20], Frame -> True, 
 PlotLegends -> Placed[legends, Below], ImageSize -> 500]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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