4
$\begingroup$

1.- How to put DASHED lines in the Legend? 2.- How to give Style to {20,Bold} to all "Expressions in Legend?

enter image description here

(I use LegendMarkSize for make lines wider than default fine style )

Plot[{{E^(x/(1/4)), E^(x/(1/3)), E^(x/(1/2))}, {E^(x/1), E^(x/2), 
   E^(x/3), E^(x/4)}}, {x, -5, 0.2}, 
 PlotStyle -> {{Dashing[0.024], Thickness[0.01]}, {Dashing[0.024], 
    Thickness[0.01]}, {Dashing[0.024], Thickness[0.01]}, 
   Thickness[0.01], Thickness[0.01], Thickness[0.01], 
   Thickness[0.01]}, 
 PlotLegends -> 
  SwatchLegend[ "Expressions", LegendMarkerSize -> {39, 6}], 
 PlotRange -> {{-5, 0.15}, {0, 1.15}}]
$\endgroup$

2 Answers 2

6
$\begingroup$

The style for labels in a legend is determined by the option LabelStyle. To get the legend markers to match the style of the plot without rescaling, one approach is to use AbsoluteDashing and AbsoluteThickness to style the plot lines.

With[{d = AbsoluteDashing[8], t = AbsoluteThickness[4]}, 
 Plot[{E^(4 x), E^(3 x), E^(2 x), E^x, E^(x/2), E^(x/3), E^(x/4)}, {x, -5, 0.2},
  PlotStyle -> {{d, t}, {d, t}, {d, t}, t, t, t, t},
  PlotRange -> {{-5, 0.15}, {0, 1.15}},
  PlotLegends -> LineLegend["Expressions",
    LabelStyle -> {Bold, 20}, LegendMarkerSize -> {40, 10}]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you, your solution is perfect!. only have to add... {Bold,20} to maintain the axis numbers bigger. Thank you $\endgroup$
    – Mika Ike
    May 3, 2014 at 15:42
3
$\begingroup$

AbsoluteDash and AbsoluteThickness are helpful in this. In the following I have arbitrarily chosen colours for illustrative purposes (so you can change as desired):

leg = {{Red, AbsoluteDashing[5], AbsoluteThickness[2]}, {Green, 
    AbsoluteDashing[5], AbsoluteThickness[2]}, {Blue, 
    AbsoluteDashing[5], AbsoluteThickness[2]}, {Red, 
    AbsoluteThickness[2]}, {Green, AbsoluteThickness[2]}, {Blue, 
    AbsoluteThickness[2]}, {Purple, AbsoluteThickness[2]}};
fns = {{E^(x/(1/4)), E^(x/(1/3)), E^(x/(1/2))}, {E^(x/1), E^(x/2), 
    E^(x/3), E^(x/4)}};
Plot[fns, {x, -5, 0.2}, PlotRange -> {{-5, 0.15}, {0, 1.15}}, 
 PlotStyle -> leg, 
 PlotLegends -> LineLegend[Directive @@@ leg, Flatten@fns]]

This yields:

enter image description here

$\endgroup$
3
  • $\begingroup$ You´re answer is TOO an SOLUTION FOR ME. I chosed the other because was posted before. $\endgroup$
    – Mika Ike
    May 3, 2014 at 13:41
  • $\begingroup$ @MikaIke thanks...that's fine I think Simon Woods is neater (note he flattened your list of expressions which were nested in your code: I flattened in my legend) $\endgroup$
    – ubpdqn
    May 3, 2014 at 13:45
  • $\begingroup$ (+1), is there any way to adjust the length of lines in the legend? $\endgroup$ Sep 19, 2019 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.