12
$\begingroup$

I have a plot with many functions. Some of the functions overlap. In Maple, when you plot functions with legends, you can just click on the legend and it highlights the corresponding line. Does something similar exist in Mathematica?

$\endgroup$
15
$\begingroup$

SetterBar

colors = ColorData[1, "ColorList"][[;; 5]]; 

Dynamic[Legended[
  Plot[Evaluate[Table[i Sin[i x], {i, 1, 5}]], {x, 0, 2 π}, 
   PlotStyle -> ReplacePart[Table[{colors[[i]], Thin}, {i, 5}],
      {Alternatives @@ n, 2} -> Thickness[.01]]], 
  SetterBar[Dynamic[n], Table[i -> 
    Grid[{{Pane[Style[Sin[i x], 16, Bold, "Panel", Opacity[1], colors[[i]], 
          Background -> Opacity[0]], ImageSize -> {60, 30}], 
        Plot[Sin[i x], {x, 0, 2 π}, PlotStyle -> colors[[i]], 
         Axes -> False, ImageSize -> 40]}}], {i, 1, 5}], 
   Appearance -> "Vertical"]]]

enter image description here

TogglerBar

Replace SetterBar above with TogglerBar:

enter image description here

In versions prior to version 11., we can also use

Dynamic[Plot[Evaluate[Table[i Sin[i x], {i, 1, 5}]], {x, 0, 2 π}, 
  PlotStyle -> ReplacePart[Table[{colors[[i]], Thin}, {i, 5}], 
      {Alternatives @@ n, 2} -> Thickness[.01]], 
  PlotLegends -> SetterBar[Dynamic[n], Table[i -> 
      Grid[{{Pane[Style[Sin[i x], 16, Bold, "Panel", Opacity[1], colors[[i]], 
           Background -> Opacity[0]], ImageSize -> {60, 30}], 
         Plot[Sin[i x], {x, 0, 2 π}, PlotStyle -> colors[[i]], 
          Axes -> False, ImageSize -> 40]}}], {i, 1, 5}], 
    Appearance -> "Vertical"]]]

to get the same result (Similarly, for TogglerBar).

$\endgroup$
5
  • $\begingroup$ That is awesome!!! Thanks kglr. I appreciate it. This is exactly what I was looking for. $\endgroup$
    – Karim
    Jan 19 '18 at 15:10
  • $\begingroup$ @Karim, my pleasure. Thank you for the accept. Welcome to mma.se. $\endgroup$
    – kglr
    Jan 19 '18 at 15:11
  • $\begingroup$ On my system, using Mathematica 11.2 under macOS, the setter and toggle bars are arrayed horizontally, rather than, as your images show, vertically. What's wrong? $\endgroup$
    – murray
    Jan 19 '18 at 22:58
  • $\begingroup$ @murray, i am using version 9 on windows 10. I just confirmed the same issue on version 11 on Wolfram Cloud. A workaround is to use Legended instead of PlotLegends: ... $\endgroup$
    – kglr
    Jan 19 '18 at 23:12
  • 1
    $\begingroup$ ... that is, Dynamic[Legended[Plot[Evaluate[Table[i Sin[i x], {i, 1, 5}]], {x, 0, 2 \[Pi]}, PlotStyle -> ReplacePart[Table[Thin, {5}], Thread[ n -> Thickness[.01]]]],TogglerBar[Dynamic[n], Table[i -> Grid[{{Pane[ Style[Sin[i x], 16, Bold, "Panel", Opacity[1], colors[[i]], Background -> Opacity[0]], ImageSize -> {60, 30}], Plot[Sin[i x], {x, 0, 2 \[Pi]}, PlotStyle -> colors[[i]], Axes -> False, ImageSize -> 40]}}], {i, 1, 5}], Appearance -> "Vertical"] ]] works as expected. $\endgroup$
    – kglr
    Jan 19 '18 at 23:12

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.