# PlotLegend frame disappears during Export

Bug introduced in 11.2 or earlier and persisting through 12.0

I placed a framed PlotLegend and it works just fine in the notebook, but after exported to PDF the legend frame disappeared.

Here's the data as example.

list={{{2, 2, 2, 2}, {10.9323, 10.9323, 10.9323,
10.9323}, {4.233}}, {{21.8656, 358.919}, {21.9156,
103.891}, {21.9656, -21.9377}, {22.0156, -52.9273}, {22.0656,
-32.5523}, {22.1156, 9.22699}, {22.1656, 54.4444}, {22.2156,
94.4478}, {22.2656, 124.699}, {22.3156, 143.77}, {22.3656,
152.507}, {22.4156, 153.301}, {22.4656, 147.381}, {22.5156,
136.734}, {22.5656, 122.683}, {22.6156, 107.942}, {22.6656,
92.2245}, {22.7156, 76.9519}, {22.7656, 63.8046}, {22.8156,
51.9468}, {22.8656, 42.5287}}}


And the plot function

fig =
ListPlot[Select[Re@#[[2]], # \[Element] Reals &] & /@ #,
PlotRange -> {{Min[#1] - 0.1,
Max[#2] -
0} &@(Sequence @@ (Transpose[{First[#[[2]]][[1]] - 0.1,
Last[#[[2]]][[1]]} & /@ #])), All}, Joined -> True,
ImageSize -> 500,
PlotLegends ->
Placed[LineLegend[(ToString[#[[1, 1, 1]]] <> "+" <>
ToString[#[[1, 1, 2]]] <> "\[Rule]" <>
ToString[#[[1, 1, 3]]] <> "+" <>
ToString[#[[1, 1, 4]]]) & /@ #,
LegendLayout -> {"Column", 1}(*,LegendMarkerSize\[Rule]20*),
LegendMargins -> 3, LegendFunction -> "Frame"], {Right,
Top}], Frame -> True]&@ {list}


and now export

Export["~/Desktop/3.pdf", fig]


It's probably not caused by the exporting format because I tried eps but had no effect.

The figure before export:

The figure after export:

BTW my MMA version is 11.2.

• Hello, did you figure out what happened? Same thing is happening to me. Would appreciate if you could share a hint of what's going on! thanks. Apr 22 '20 at 23:09

Export is clearly not working correctly. One thing that works for me is:

1. click the graphic in your notebook to select it
2. right-click to bring up the context menu
3. "Save Graphic As..." and choose PDF.

This preserves the frame around the legend.

You could also try my autoLegend function from this answer. It's from the bad old days before PlotLegends were introduced in Mathematica, but it still works fine for me, and also exports properly.

I don't know what's wrong with the export,but a little bit change will do the job right. You could change LegendFunction -> "Frame" to LegendFunction -> (Framed[#, RoundingRadius -> 4]&) and run it again.

 Export["TestPlot18" <> #, fig, ImageResolution -> 300] & /@ {".pdf", ".png", ".jpg"}


• I don't understand. I presume the only difference is ImageResolution? But I added it and it didn't work. Well, png works but that's the same as Rasterize I think? And it would looks really bad inside a pdf file, I mean, with latex. Mar 12 '18 at 14:04
• But, we found that its works well for pdf and jpg same ag png. We can sent you if you want. Mar 12 '18 at 14:53
• That's weird. What's your MMA version? And thank you for your kindness but I want to solve this problem, at least find out what went wrong. Mar 12 '18 at 15:28
• I am using MMA 10. There is typo in your list data (inserted / at {22.0656, \-32.5523} this point) . I found only one plot. as shown above. Mar 12 '18 at 15:53

You can click before the figure (Fig). Then write Export["Fig is here",eps]. Using this method you can export all parts of your figure.

• The legend frame would become "Unevaluated["the legend"]". Sep 4 '19 at 6:28
• I cannot run your fig. There are some errors. Sep 5 '19 at 8:38
• There was an extra line break last line, it should be fine now. Sep 6 '19 at 6:43