Message Boards Message Boards

Is there a way to toggle curves in a plot?

Posted 10 years ago

I have 6 functions that have multiple parameters defined to be used in Manipulate []. I am plotting these functions all on one plot but I would like to be able to turn each one on and off (toggle). Is there a simple way (or better yet....a Mathematica defined function) to do this?

Attachments:
POSTED BY: Tim Kirkpatrick
9 Replies

All examples were written and tested in desktop Mathematica and seemed to work just fine. All bets are off when trying interactive things out in Mathematica online at this point. So, if Mathematica online gives you an error message and substitutes something for what was originally intended then it's pretty unlikely that the originally designed behaviors will work correctly. But it is worth sending in a report if you wish to move from support to notify them about this bug.

POSTED BY: David Reiss
Posted 10 years ago

I had to try these examples in Mathematica Online (currently beta):

David Reiss' first example gave an error ("Manipulate::vstype: Manipulate does not support ControlType -> CheckboxBar. ControlType -> SetterBar will be used instead.") but still worked. Three plots showed up, but as I toggled through the buttons, only one curve would show. I couldn't "untoggle" any selections, hoping that all three curves would display themselves again. (I'm at work and don't have access to desktop Mathematica. Does this code work in desktop Mathematica such that one can "untoggle" an individual plot and therefore bring back all three plots?)

Eric Schulz' example yielded all three plots. Same error message regarding CheckboxBar, but the plots show up anyway. However, clicking the first selection button got rid of all three plots and none would show up when cycling through the other buttons. Re-executing the code brings them back.

FYI.

POSTED BY: Glen Deering

Here is another approach to maintaining the same plot style for each graph independent of which set of checkboxes are checked:

Manipulate[
 Plot[Evaluate[
   choices /. {1 -> Sin[x], 2 -> Cos[x], 3 -> BesselJ[3, x]}], {x, 0, 
   10},
  PlotRange -> {All, {-1.5, 1.5}},
  PlotStyle -> {Directive[Red, Thick], Directive[Blue, Dashed], 
     Directive[Green, Medium]}[[choices]]], {{choices, {1, 2, 3}}, {1,
    2, 3}, CheckboxBar}]
POSTED BY: David Reiss
POSTED BY: Tim Kirkpatrick
Posted 10 years ago

Here is another solution that uses Checkboxes to display function choices as suggested by David R and David P, and ensures the viewing window and axes remain unchanged (specifying an explicit PlotRange is still a good idea). Note that a PlotStyle for each function can easily be specified with this technique.

Manipulate[
 Plot[{5 Sin[x], Cos[x], BesselJ[3, x]},{x, 0, 10},
  PlotStyle -> {
    If[MemberQ[choices, Dashed}, None],
    If[MemberQ[choices, 2], Automatic, None],
    If[MemberQ[choices, 3], Purple, None]}],
 {{choices, {1, 2, 3}}, {1, 2, 3}, CheckboxBar}]
POSTED BY: Eric Schulz

You are exactly right on all counts David. ...as usualÂ…. ;-)

POSTED BY: David Reiss

Here is an example of an approach:

Manipulate[
 Plot[
 Evaluate[
   choices /. {1 -> Sin[x], 2 -> Cos[x], 3 -> BesselJ[3, x]}], {x, 0, 
   10}],

 {{choices, {1, 2, 3}}, {1, 2, 3}, CheckboxBar}
 ]
POSTED BY: David Reiss
POSTED BY: S M Blinder
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract