# Create legend of a line with multiple sections and multiple colors

I have this plot. The first three lines are considered as one curve. I want to make a legend something like this: 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]

• Perhaps use Callout?
– Alan
Aug 16 at 17:58

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[], AbsoluteThickness,
Line[{{0, 0}, {1/3, 0}}]}, {style[], AbsoluteThickness,
Line[{{1/3, 0}, {2/3, 0}}]}, {style[], AbsoluteThickness,
Line[{{2/3, 0}, {1, 0}}]}}];
markers2 = Graphics[{{style[], Line[{{0, 0}, {.8, 0}}]}}];
Legended[plot,
Placed[SwatchLegend[{Opacity, Opacity}, {"curve 1", "curve 2"},
LegendMarkers -> {markers1, markers2},
LegendMarkerSize -> {40, 20},
LegendFunction -> (Framed[#, RoundingRadius -> 4,
FrameStyle -> Brown] &)], {.3, .8}]] 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][], Line[{{0, 0}, {.1, 0}}]},
{style[t][], Line[{{.1, 0}, {.2, 0}}]},
{style[t][], Line[{{.2, 0}, {.3, 0}}]}
}, ImageSize -> 50, PlotRange -> {Automatic, {-.1, .1}}
];
p2 = Graphics[{{style[t][], 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, "Curve 1"}],
Row[{p2, Spacer, "Curve 2"}]}], Right]] 