13
$\begingroup$

The new legends functionality in version 9 really simplifies creating legends, and it automatically picks up the styles of the lines.

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, 
 PlotStyle -> {Directive[AbsoluteThickness[3], Orange], 
   Directive[AbsoluteThickness[3], Brown]}, PlotLegends -> LineLegend["Expressions"]]

Nice fat lines

But if there is a BaseStyle option to the plot affecting how the lines are plotted, the legend doesn't pick that up. See how the lines in the legend are now skinnier than in the plot.

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, BaseStyle -> AbsoluteThickness[3], 
 PlotStyle -> {Orange, Brown},  PlotLegends -> LineLegend["Expressions"]]

Oh noes! The lines are skinny!

How can I get LineLegend to see the BaseStyle of the plot while maintaining the automatic functionality of not having to explicitly specify the colors used in the plot?

$\endgroup$

2 Answers 2

17
$\begingroup$

You can't actually tell the LineLegends construct what the BaseStyle option of the enclosing plot is. But you can set the BaseStyle option for the LineLegend as well. Do not be fooled by the red text when you type BaseStyle inside the LineLegend! This is just another of those cases where things work even though they are not documented and show up in red.

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, BaseStyle -> AbsoluteThickness[3], 
 PlotStyle -> {Orange, Brown}, 
 PlotLegends ->  LineLegend["Expressions", BaseStyle -> AbsoluteThickness[3]]]

Which actually looks like this:

enter image description here

Produces this, just as required.

yay! fat lines again!

$\endgroup$
5
$\begingroup$

This bug was fixed in version 10.

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, 
 BaseStyle -> AbsoluteDashing[{5, 6}], PlotStyle -> {Orange, Brown}, 
 PlotLegends -> LineLegend["Expressions"]]

enter image description here

However, AbsoluteThickness does not seem to propagate as a BaseStyle. This might have something to do with the new plot themes.

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, BaseStyle -> AbsoluteThickness[15], 
 PlotLegends -> LineLegend["Expressions"]]

enter image description here

If you explicitly set PlotTheme -> None, the BaseStyle works both for the lines and for the legend.

Plot[{Sin[x], Cos[x]}, {x, 0, 5}, PlotTheme -> None, 
 BaseStyle -> AbsoluteThickness[5], 
 PlotLegends -> LineLegend["Expressions"]]

enter image description here

$\endgroup$

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.