20
$\begingroup$

I've been using the shiny new legends in V9 for my plots. However, I usually increase the Frame/Axis Thickness and the plot Thickness and I would like the thickness of the legend line to match. There is a related question (Legend of a plot: how to increase the size of the line/marker?) asked about using the previous PlotLegends package.

See this simple example, modified from the documentation:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> Automatic, PlotStyle -> Thickness[0.005], AxesStyle -> Thickness[0.005]] 

Mathematica graphics

I've tried the obvious modification of the LineLegend LabelStyle, however, this just appears to modify the text and has no effect here:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> LineLegend[Automatic, LabelStyle -> Thickness[0.5]], PlotStyle -> Thickness[0.005], AxesStyle -> Thickness[0.005]] 

Mathematica graphics

I found that I could set Joined -> False and use a thicker line as a LegendMarker but it is never as long as the normal legend line no matter how long you make the line... (also note that to get a comparable thickness it has to be set two orders of magnitude higher)

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> LineLegend[Automatic, Joined -> False, LabelStyle -> Directive[FontFamily -> "Helvetica", Medium], LegendMarkers -> Graphics[{Thickness[0.5], Line[{{-2, 0}, {2, 0}}]}]], PlotStyle -> Thickness[0.005], AxesStyle -> Thickness[0.005]] 

Mathematica graphics

now make the line really long (no change):

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> LineLegend[Automatic, Joined -> False, LabelStyle -> Directive[FontFamily -> "Helvetica", Medium], LegendMarkers -> Graphics[{Thickness[0.5], Line[{{-100, 0}, {100, 0}}]}]], PlotStyle -> Thickness[0.005], AxesStyle -> Thickness[0.005]] 

Mathematica graphics

I've tried to look at the InputForm for the plots and for just a LineLegend but I can't figure out what exactly I need to replace to fix this.

$\endgroup$
2
  • $\begingroup$ Note, the boxed 1 and 2 are placeholders that can be edited. Personally, I prefer "Expressions" to Automatic as it will display the equations used, for function plots, only, of course. $\endgroup$ Commented Mar 15, 2013 at 3:02
  • $\begingroup$ I think you're looking for LegendMarkerSize. See also: mathematica.stackexchange.com/q/19283/8 $\endgroup$ Commented Mar 15, 2013 at 4:08

4 Answers 4

13
$\begingroup$

I find it useful to stick with AbsoluteThickness (and related) when using PlotLegends.

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> Automatic, PlotStyle -> AbsoluteThickness[2], AxesStyle -> AbsoluteThickness[2]] 

enter image description here

$\endgroup$
10
$\begingroup$

If you remove Joined->False it seems that you can get a better length for the given line thickness:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> LineLegend[Automatic, LabelStyle -> Directive[FontFamily -> "Helvetica", Medium], LegendMarkers -> Graphics[{Thickness[0.2], Line[{{-10, 0}, {10, 0}}]}]], PlotStyle -> Thickness[0.005], AxesStyle -> Thickness[0.005]] 

thicker and longer lines

$\endgroup$
9
$\begingroup$

As a last resort you can always adjust LineLegend manually:

stl[n_, thick_] := Thread[{ColorData[97, "ColorList"][[;; n]], AbsoluteThickness@thick}]; leg = LineLegend[Directive@@@stl[2, 10], Automatic]; Plot[{Sin[x], Cos[x]}, {x, 0, \[Pi]}, PlotLegends -> leg] 

enter image description here

I'm quite surprised that one can't do it in more usual way..

$\endgroup$
5
$\begingroup$
frame[legend_] :=Framed[legend, FrameStyle -> {Black, Thickness[1.]}, RoundingRadius -> 10, FrameMargins -> 2, Background -> White]; Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotStyle -> {{Orange, AbsoluteThickness[3]}, {Blue, Dashed, AbsoluteThickness[3]}}, PlotLegends ->Placed[LineLegend[{Style["Experimental Results", 17], Style["Numeric Results", 17]}, LegendFunction -> frame], {1.1, 0.5}], ImageSize -> 500] 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.