# Switching PlotLegends according to CheckboxBar selection

In using Manipulate to illustrate selectively function dynamics I encountered the following difficulty with the PlotLegends option. There are three plots, which can be selected through the 'function' variable check boxes. They display separately or in combination. The PlotLegends option, however, only displays the legend labels in order, starting from the first one, regardless of which 'function' boxes are selected.

Manipulate[Plot[Evaluate[function /. t -> tt], {x, -2 Pi, 2 Pi}, PlotRange -> {-2, 2},
PlotLegends -> {"Re[e^i(x-t)+e^i(x+t)]", "Re[e^i(x-t)]", "Im[e^i(x+t)]"}],
{{function, {Re[Exp[I (x - t)] + Exp[I (x + t)]]}},
{Re[Exp[I (x - t)] + Exp[I (x + t)]] -> "Re[e^i(x-t)+e^i(x+t)]",
Re[Exp[I (x - t)]] -> "Re[e^i(x-t)]", Im[Exp[I (x + t)]] -> "Im[e^i(x+t)]"},
CheckboxBar}, {{tt, 0, "t = 0 to 2Pi "}, 0, 2 Pi}]


Two illustrating images, showing mismatched function selection and plot legends, are included below.  I shall be grateful for any suggestions how to set the plot legends, so that they always correspond correctly to any combination of the function plot selections. (There could be a trivial answer, but I have not succeeded in finding it.)

• try using PlotLegends -> function?
– kglr
Jan 24 at 11:55
• Thank you kglr. I would not have worked out how exactly, but Daniel's answer was clear and helpful. Now I understand what you meant. Jan 24 at 16:03

Clear["Global*"]


The order of the functions in the legend should remain constant as the selections change. Likewise, the color associated with each function should remain constant. To accomplish this, the CheckboxBar values should be indices.

funcs = {
Re[Exp[I (x - t)] + Exp[I (x + t)]],
Re[Exp[I (x - t)]],
Im[Exp[I (x + t)]]};

colors = ColorData /@ Range[Length[funcs]];

Manipulate[
function = Sort@function (* keeps legends in consistent order *);
Plot[
Evaluate[funcs[[function]] /. t -> tt],
{x, -2 Pi, 2 Pi},
PlotStyle -> colors[[function]] (* keeps colors consistent *),
PlotRange -> {-2, 2},
PlotLegends -> {funcs[[function]]}],
{{function, {1, 2, 3}},
CheckboxBar},
{{tt, 0, "t = 0 to 2Pi "}, 0, 2 Pi, Appearance -> "Labeled"}] • Most impressive! My appreciation is much more than the +1 I am limited to. Thank you, Bob. Jan 25 at 8:41

You must specify as many labels as there are functions to plot:

Manipulate[
Plot[Evaluate[function /. t -> tt], {x, -2 Pi, 2 Pi},
PlotRange -> {-2, 2},
PlotLegends ->
Evaluate[
function /. {Re[Exp[I (x - t)] + Exp[I (x + t)]] ->
"Re[e^i(x-t)+e^i(x+t)]", Re[Exp[I (x - t)]] -> "Re[e^i(x-t)]",
Im[Exp[I (x + t)]] -> "Im[e^i(x+t)]"}]]
, {{function, {Re[Exp[I (x - t)] + Exp[I (x + t)]]}}, {Re[
Exp[I (x - t)] + Exp[I (x + t)]] -> "Re[e^i(x-t)+e^i(x+t)]",
Re[Exp[I (x - t)]] -> "Re[e^i(x-t)]",
Im[Exp[I (x + t)]] -> "Im[e^i(x+t)]"},
CheckboxBar}, {{tt, 0, "t = 0 to 2Pi "}, 0, 2 Pi}]
` • Thank you very much, Daniel. I was not aware that PlotLegends can take such definitions. Works exactly as needed. Most helpful! Jan 24 at 16:01