3
$\begingroup$

It would be nice to be able to include a legend in a plot which has grid lines, but not have the grid lines included in the legend box. Here is an example with the legend containing those grid lines:

a[t_, \[Omega]_, \[Lambda]_] := Exp[-\[Lambda] t/5]; l = 
ConstantArray[0, 5];
plot[q_, legend_, title_] := 
Plot[q, {t, 0, 1}, PlotLegends -> legend, Frame -> True, 
GridLines -> Automatic, PlotRange -> {-0.025, 1.025}, 
FrameLabel -> {"t [seconds]", "F(t)", title}, 
LabelStyle -> Directive[14]]

l = Table[
   Style[StringJoin["\[Lambda] = ", ToString[\[Lambda]]], 
   FontFamily -> "Apple Chancery", FontSlant -> Italic], {\[Lambda], 
   Range[5]}];
legend = 
   Placed[LineLegend[l, LegendFunction -> "Frame"], {.225, .35}];

plot[Table[
   a[t, 1, \[Lambda]], {\[Lambda], Range[5]}], legend, "An example"]

enter image description here

$\endgroup$

1 Answer 1

7
$\begingroup$
LegendFunction -> (Framed[#, FrameMargins -> 0, 
     Background -> White] &)]

enter image description here

$\endgroup$
4
  • 2
    $\begingroup$ +1 However, the Legend may be too prominent. I recommend (Framed[#, FrameMargins -> 0, RoundingRadius -> 4, Background -> White, FrameStyle -> Gray] &) $\endgroup$
    – Bob Hanlon
    May 4 at 1:10
  • 1
    $\begingroup$ An important question that has raised to me myself: in the above figure how we can change the length of the lines in the legend. It is so important case when we have several Dashing styles, in our plots. Sometimes because of short length of lines in the legends we cannot distinguish different legends from each other. $\endgroup$ May 4 at 1:27
  • 1
    $\begingroup$ @InzoBabaria - Add the option PlotTheme -> "Monochrome" to the Plot command. Are those lines long enough? $\endgroup$
    – Bob Hanlon
    May 4 at 1:39
  • $\begingroup$ Thank you very much for the answer and the additional discussion and improvements in the comments! $\endgroup$
    – mjw
    May 4 at 2:16

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.