Message Boards Message Boards


Is there a way to toggle curves in a plot?

Posted 9 years ago
9 Replies
15 Total Likes

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?

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 9 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.


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:

   choices /. {1 -> Sin[x], 2 -> Cos[x], 3 -> BesselJ[3, x]}], {x, 0, 
  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

Thank you, all, for all the good tips! I have tried writing a "toggle" function for a while now and thought it was time to open it up to the Mathematica community to get feedback.

There is one fundamental flaw when using Evaluate[] within the Plot[] function; that whenever a graph is turned off, the PlotStyle-> changes for each curve based on whatever plot is open and the order of the PlotStyle-> definitions. This is a problem for all other ways I have come up with to toggle plots as well, though.

My attempts with this have focused on writing a command in the PlotStyle-> similar to what was suggested by Eric (see above). I was not familiar with the MemberQ[] function until now, though. However, after looking it up, I'm still not certain what it is doing. Can someone explain the MemberQ[] function and describe what it does?

Thanks again for the help!

POSTED BY: Tim Kirkpatrick
Posted 9 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.

 Plot[{5 Sin[x], Cos[x], BesselJ[3, x]},{x, 0, 10},
  PlotStyle -> {
    If[MemberQ[choices, 1], {Red, 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. usualÂ…. ;-)

POSTED BY: David Reiss

Nice example David!

I would like to strongly endorse the idea of using Checkboxes to control the material presented in a graphic. It is an extremely powerful technique that allows multiple (but related) presentations within one space. It's a way to both relate and to unclutter various elements. This is why a dynamic mediums such as Mathematica or CDF files are far superior to PDF files or printed papers.

There is just one small suggestion I would add to David's example. A dynamic graphic should always have a fixed background, which in this case means fixed axes. Otherwise a certain amount of disorientation occurs as to what it is that is changing. Adding an explicit PlotRange option will fix that.

Here is an example of an approach:

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

 {{choices, {1, 2, 3}}, {1, 2, 3}, CheckboxBar}
POSTED BY: David Reiss

Couldn't access the attachment. But here is how you might be able to select which curves to display:

Plot[{c1 f1[x], c2 f2[x], . . .}. . . Then have manipulate controls so that you can toggle between c1=0 or 1, c2=0 or 1, etc.

POSTED BY: S M Blinder
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract