Update 2: We can combine a toggler-bar legend and FlipView
to control curve visibility by clicking on curves and/or legends. (Naturally this approach is less responsive than either method.)
f[a_, b_, c_] := (a x^2 + b x + c)/3;
tup1 := RandomInteger[5, {40, 3}];
tup = tup1;
functions = f @@@ tup;
colors = ColorData[97] /@ Range[Length @ functions];
functionswithtooltips = MapThread[Tooltip, {functions, tup}];
First we plot functionswithtooltips
and extract the primitives:
plot1 = Plot[Evaluate[functionswithtooltips], {x, 0, 5},
GridLines -> Automatic, ImageSize -> Large,
PlotStyle -> Thread[Directive[colors, Opacity[.3], Thin]]];
ttlist = Cases[plot1, _Tooltip, All];
Note: In versions 13.3+, add the option PlotHighlighting -> None
in definition of plot1
above.
Then we turn each tooltipped line into an EventHandler
and add the necessary updating to dynamic variables:
Deploy @ DynamicModule[{n = {1}, hidden = ConstantArray[1, Length @ ttlist]},
hidden[[n]] = 2;
Dynamic[Legended[
Graphics[Table[With[{i = i, ind = hidden[[i]]},
Tooltip[{ttlist[[i, 1, 1]],
DynamicModule[{boxes = {ttlist[[i, 1, 2]],
Style[ttlist[[i, 1, 2]], Opacity[1], Thickness[Large]]},
index = ind, length = 2},
EventHandler[Dynamic[boxes[[index]]],
{"MouseClicked" :> (index = index /. {1 -> 2, 2 -> 1};
hidden[[i]] = index; n = Flatten[Position[hidden, 2]];),
Method -> "Preemptive",
PassEventsDown -> True, PassEventsUp -> True}],
DynamicModuleValues :> {}]},
ttlist[[i, 2]]]], {i, 1, Length @ ttlist}],
ImageSize -> 700, AspectRatio -> 1/GoldenRatio,
PlotRange -> {{0, 5}, {0, 50}}, Frame -> True],
TogglerBar[Dynamic[n, (n = #; hidden[[All]] = 1; hidden[[n]] = 2) &],
MapIndexed[
#2[[1]] -> Style[#, 16, Bold, Opacity[1], colors[[#2[[1]]]]] &, tup],
Appearance -> "Vertical" -> {Automatic, 4}]]]]
Update: We can use FlipView
to interactively hide/show a curve:
plot1 = Plot[Evaluate[functionswithtooltips], {x, 0, 5},
GridLines -> Automatic, ImageSize -> Large,
PlotStyle -> Thread[Directive[colors, Opacity[.3], Thin]]];
plot1 /. l_Line :>
FlipView[{l, Style[l, Opacity[1], Thick]}] // Deploy
Original answer:
You can use TogglerBar
as legend panel to hide/show any subset of lines:
f[a_, b_, c_] := (a x^2 + b x + c)/3;
tup1 := RandomInteger[5, {40, 3}];
tup = tup1;
functions = f @@@ tup;
colors = ColorData[97] /@ Range[Length @ functions];
Dynamic[Legended[Plot[functions, {x, 0, 5}, GridLines -> Automatic,
ImageSize -> Large,
PlotStyle -> ReplacePart[colors, {Except[Alternatives @@ n]} -> None]],
TogglerBar[Dynamic[n],
MapIndexed[
#2[[1]] -> Style[#,16, Bold, Opacity[1], colors[[#2[[1]]]]] &, tup],
Appearance -> "Vertical" -> {Automatic, 4}]]]
You can add tooltips and play with opacity and thickness rather than making lines completely invisible:
functionswithtooltips = MapThread[Tooltip, {functions, tup}];
DynamicModule[{n = {1}},
Dynamic[Legended[Plot[Evaluate @ functionswithtooltips , {x, 0, 5},
GridLines -> Automatic, ImageSize -> Large,
PlotStyle -> (Thread[Directive[colors, Opacity[.3], Thin]] /.
Directive[a : Alternatives @@ colors[[n]], _, _] :>
Directive[a, Opacity[1], AbsoluteThickness[3]])],
TogglerBar[Dynamic[n],
MapIndexed[
#2[[1]] -> Style[#, 16, Bold, Opacity[1], colors[[#2[[1]]]]] &, tup],
Appearance -> "Vertical" -> {Automatic, 4}]]]]