3
$\begingroup$

I have a plot with rounded dashes, but LineLegend seems to be unable to draw them.

style = {Directive[Black], 
       Directive[Red, Opacity[.5], 
        Dashing[{.02, .015}, 0, CapForm["Round"]]], 
       Directive[Blue, Opacity[.5], 
        Dashing[{.005, .01}, 0, CapForm["Round"]]]};

Looks good in a plot:

Plot[{Sin[x], Cos[x], E^(-x)}, {x, 0, 2 Pi}, PlotStyle -> style]

But not in a line legend:

LineLegend[style, {"1", "2", "3"}]

For me (V13.2.1.0) the line legend has no dashes at all, rounded or not. If I remove the rounding from style, the dashes at least appear with sharp edges, but I want the rounded ones in the plot and I want the legend to match the plot. Is this a bug, and if so, is there a workaround?

$\endgroup$

1 Answer 1

5
$\begingroup$

The graphics object in the legend is too small for the dashings to be visible. You can see this using a larger LegendMarkerSize:

LineLegend[style, {"1", "2", "3"}, LegendMarkerSize -> {300, 20}]

enter image description here

Two work-arounds:

1. To get visible dashing in the legend modify style to increase the first argument in Dashing:

LineLegend[style /. Dashing[a_, b___] :> Dashing[10 a, b], {"1", "2", "3"}]

enter image description here

2. Alternatively, specify dashing in absolute lengths (independent of image size of the parent graphics) using AbsoluteDashing instead of Dashing:

style2 = {Directive[Black], 
   Directive[Red, Opacity[.5], 
    AbsoluteDashing[{10, 5}, 0, CapForm["Round"]]], 
   Directive[Blue, Opacity[.5], 
    AbsoluteDashing[{5, 3}, 0, CapForm["Round"]]]};

Plot[{Sin[x], Cos[x], E^(-x)}, {x, 0, 2 Pi}, PlotStyle -> style2]

enter image description here

LineLegend[style2, {"1", "2", "3"}]

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.