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
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, 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. ...as 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:

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

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
Attachments
Remove
or Discard

Group Abstract Group Abstract