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], 
legend = 
   Placed[LineLegend[l, LegendFunction -> "Frame"], {.225, .35}];

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

enter image description here


1 Answer 1

LegendFunction -> (Framed[#, FrameMargins -> 0, 
     Background -> White] &)]

enter image description here

  • 2
    $\begingroup$ +1 However, the Legend may be too prominent. I recommend (Framed[#, FrameMargins -> 0, RoundingRadius -> 4, Background -> White, FrameStyle -> Gray] &) $\endgroup$
    – Bob Hanlon
    Commented May 4, 2022 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$ Commented May 4, 2022 at 1:27
  • 1
    $\begingroup$ @InzoBabaria - Add the option PlotTheme -> "Monochrome" to the Plot command. Are those lines long enough? $\endgroup$
    – Bob Hanlon
    Commented May 4, 2022 at 1:39
  • $\begingroup$ Thank you very much for the answer and the additional discussion and improvements in the comments! $\endgroup$
    – mjw
    Commented May 4, 2022 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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