5
$\begingroup$

I have this plot. The first three lines are considered as one curve.

Enter image description here

I want to make a legend something like this:

Enter image description here

How can I do this?

style = {Directive[Thickness[0.03], ColorData[97, 1], 
    CapForm["Round"], Opacity[0.3]], 
       Directive[Thickness[0.03], ColorData[97, 2], CapForm["Round"], 
    Opacity[0.3]], 
       Directive[Thickness[0.03], ColorData[97, 3], CapForm["Round"], 
    Opacity[0.3]], ColorData[97, 4]}; 
Plot[{ConditionalExpression[x, x <= 1], 
  ConditionalExpression[2 - x, 1 <= x <= 3/2], 
     ConditionalExpression[2*x^2 - 4, 3/2 <= x <= 2], (2 + x^2)/
   5}, {x, 0, 3}, PlotStyle -> style]
$\endgroup$
1
  • $\begingroup$ Perhaps use Callout? $\endgroup$
    – Alan
    Aug 16 at 17:58

2 Answers 2

7
$\begingroup$
style = {Directive[Thickness[0.03], ColorData[97, 1], 
    CapForm["Round"], Opacity[0.3]], 
   Directive[Thickness[0.03], ColorData[97, 2], CapForm["Round"], 
    Opacity[0.3]], 
   Directive[Thickness[0.03], ColorData[97, 3], CapForm["Round"], 
    Opacity[0.3]], ColorData[97, 4]};
plot = Plot[{ConditionalExpression[x, x <= 1], 
   ConditionalExpression[2 - x, 1 <= x <= 3/2], 
   ConditionalExpression[2*x^2 - 4, 3/2 <= x <= 2], (2 + x^2)/5}, {x, 
   0, 3}, PlotStyle -> style]
markers1 = 
  Graphics[{{style[[1]], AbsoluteThickness[4], 
     Line[{{0, 0}, {1/3, 0}}]}, {style[[2]], AbsoluteThickness[4], 
     Line[{{1/3, 0}, {2/3, 0}}]}, {style[[3]], AbsoluteThickness[4], 
     Line[{{2/3, 0}, {1, 0}}]}}];
markers2 = Graphics[{{style[[4]], Line[{{0, 0}, {.8, 0}}]}}];
Legended[plot, 
 Placed[SwatchLegend[{Opacity[1], Opacity[1]}, {"curve 1", "curve 2"},
    LegendMarkers -> {markers1, markers2}, 
   LegendMarkerSize -> {40, 20}, 
   LegendFunction -> (Framed[#, RoundingRadius -> 4, 
       FrameStyle -> Brown] &)], {.3, .8}]]

enter image description here

$\endgroup$
5
$\begingroup$

I just saw the answer above, but might as well post my attempt.

style[t_] := {Directive[Thickness[t], ColorData[97, 1], 
    CapForm["Round"], Opacity[0.3]],
   Directive[Thickness[t], ColorData[97, 2], CapForm["Round"], 
    Opacity[0.3]],
   Directive[Thickness[t], ColorData[97, 3], CapForm["Round"], 
    Opacity[0.3]],
   ColorData[97, 4]
   };
c1 = ConditionalExpression[x, x <= 1];
c2 = ConditionalExpression[2 - x, 1 <= x <= 3/2];
c3 = ConditionalExpression[2*x^2 - 4, 3/2 <= x <= 2];
t = 0.15; (*change to desired thinking for legend only*)
p = Graphics[{{style[t][[1]], Line[{{0, 0}, {.1, 0}}]},
    {style[t][[2]], Line[{{.1, 0}, {.2, 0}}]},
    {style[t][[3]], Line[{{.2, 0}, {.3, 0}}]}
    }, ImageSize -> 50, PlotRange -> {Automatic, {-.1, .1}}
   ];
p2 = Graphics[{{style[t][[4]], Line[{{0, 0}, {.3, 0}}]}
    }, ImageSize -> 50, PlotRange -> {Automatic, {-.1, .1}}
   ];
Legended[
 Plot[{c1, c2, c3, (2 + x^2)/5}, {x, 0, 3}, PlotStyle -> style[0.03], 
  ImageSize -> 250, GridLines -> Automatic, 
  GridLinesStyle -> LightGray],
 Placed[Column[{Row[{p, Spacer[5], "Curve 1"}], 
    Row[{p2, Spacer[5], "Curve 2"}]}], Right]]

Mathematica graphics

$\endgroup$

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.