7
$\begingroup$

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?

$\endgroup$
8
$\begingroup$

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"}]

enter image description here

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

enter image description here

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

enter image description here

BarLegend["Rainbow"] 

enter image description here

Non-default styles are possible as well:

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

enter image description here

(all examples taken from the respective documentation pages)

$\endgroup$
  • $\begingroup$ 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.) $\endgroup$ – wondering May 4 '15 at 12:50
7
$\begingroup$

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

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

Export["plotlegend.pdf",lineleg]

To get the default colors associated with various PlotThemes you can use the function Charting`ResolvePlotTheme:

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

enter image description here

See also: this answer


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 *)
$\endgroup$
  • $\begingroup$ Yes, this is a solution. It just seem that in the second case it should be rather Export["plotlegend.pdf", p[[2, 1]]] right? $\endgroup$ – wondering May 4 '15 at 12:51
  • $\begingroup$ @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. $\endgroup$ – kglr May 4 '15 at 13:19
  • $\begingroup$ @wondering, yes... you do need to use p[[2,1]] in version 10. $\endgroup$ – kglr May 4 '15 at 13:45
  • $\begingroup$ 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. $\endgroup$ – wondering May 4 '15 at 17:20

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.