# How to export plot legend independently?

Let us take an adapted example from Mathematica doc's:

p=Show[ListLinePlot[Table[Accumulate[RandomReal[{-1, 1}, 250]], {3}],
Filling -> Axis, PlotLegends -> {"one", "two", "three"}]]
Export["plot.pdf", p];


I will have several graphs with the same legend so I want to export it separately and use only once in my latex file. Sure, I could export all but one plot without the legend but then the size of one plot will be different, I would have to adjust fonts etc. How to achieve that?

Using either Swatchlegend, PointLegend, LineLegend, Barlegend you can easily generate a legend like you would get in a plot.

SwatchLegend[{Red, Green, Blue}, {"red", "green", "blue"}]


PointLegend[{Red, Green, Blue}, {"red", "green", "blue"}]


LineLegend[{Red, Green, Blue}, {"red", "green", "blue"}]


BarLegend["Rainbow"]


Non-default styles are possible as well:

LineLegend[{Directive[Red, Dashed],
Directive[Blue, AbsoluteThickness[3]],
Directive[Green, AbsoluteThickness[3], Dashed]}, {"label1",
"label2", "label3"}]


(all examples taken from the respective documentation pages)

• Thank you. The thing is that I don't want to explicitly write the color and I want to use the default ones from my example. I don't like the shiny red, blue an green ones. If this is possible, it would answer my question. (Another option, though less preferable, would be to find out the names of the default colors.) – wondering May 4 '15 at 12:50

Update: Generate a separate legend with the default color scheme and export it:

lineleg = LineLegend["DefaultPlotStyle"/.
(Method/. ChartingResolvePlotTheme[Automatic, ListLinePlot]),
{"leg1", "leg2", "leg3"}];

Export["plotlegend.pdf",lineleg]


To get the default colors associated with various PlotThemes you can use the function ChartingResolvePlotTheme:

 Row[Labeled[LineLegend["DefaultPlotStyle" /.
(Method/. ChartingResolvePlotTheme[#, ListLinePlot]),
{"leg1", "leg2", "leg3"}],Style[#,"Panel",14],Top]&/@
{Automatic, "Classic","Vibrant","Monochrome"},Spacer[20]]


Original post:

Export only the plots:

Export["plots.pdf", p[[ 1]]]


Export only the legend:

Export["plotlegend.pdf", p[[2, 1, 1]]]
(* Export["plotlegend.pdf", p[[2, 1]]] in version 10.0.1 *)

• Yes, this is a solution. It just seem that in the second case it should be rather Export["plotlegend.pdf", p[[2, 1]]] right? – wondering May 4 '15 at 12:51
• @wondering, in version 9.0.1.0 (windows 8 x64) p[[2,1]] gives Placed[..., After, Identity] and p[[2,1,1]] gives the legend itself. – kglr May 4 '15 at 13:19
• @wondering, yes... you do need to use p[[2,1]]` in version 10. – kglr May 4 '15 at 13:45
• Could you please help me to implement the default "Automatic" color scheme into this answer of yours? As I noted in the comment, I would like the colors to be automatically transfered into the legend. I would appreciate this and finally I could mark the question as answered. – wondering May 4 '15 at 17:20