3
$\begingroup$

I have multiple curves in my plot, and want to be able to toggle their visibility off/on. Which I got to work thanks to a different post.

data = {{1, 2, 3}, {2, 5}, {0, 6}};
Manipulate[ListPlot[data,
Joined -> True, PlotRange -> {-1, 5},
PlotStyle -> {Opacity[a], Opacity[b], Opacity[c]}],
{{a, 1, "f1"}, {1, 0}}, {{b, 1, "f2"}, {1, 0}}, {{c, 1, "f3"}, {1, 0}}]

My dataset is not always of the same dimensions though. So depending on the data loaded I might have only two graphs next time I run my notebook.

data= {{1, 3}, {0, 2}}

Is there a nice way to make my code automatically adjust to the number of graphs?

I think i could make this work using Table and Length[data], but it feels as if there is a simpler and nicer way.

I'd appreciate any help!

$\endgroup$
6
  • $\begingroup$ The number of flags is dependent of Length[data],so you'll need to use it either explicitly or implicitly $\endgroup$ Jan 14, 2015 at 15:30
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Jan 14, 2015 at 15:31
  • $\begingroup$ Closely related: (11073) $\endgroup$
    – ybeltukov
    Jan 14, 2015 at 16:27
  • $\begingroup$ @ybeltukov I am not reading carefully; how is this one distinct from that one? $\endgroup$
    – Mr.Wizard
    Jan 14, 2015 at 16:35
  • 1
    $\begingroup$ @Mr.Wizard (11073) is about toggling 3 curves exactly. The main point of this one is the variable number of curves. $\endgroup$
    – ybeltukov
    Jan 14, 2015 at 16:38

2 Answers 2

3
$\begingroup$

You can set up list of opacities automatically

data = {{1, 2, 3}, {2, 5}, {0, 6}};
Manipulate[ListPlot[data, Joined -> True, PlotRange -> {-1, 5}, 
  PlotStyle -> (Opacity@Boole@MemberQ[x, #] & /@ Range@Length@data)], 
    {{x, {1}}, Dynamic@Range@Length@data, ControlType -> TogglerBar}]

enter image description here

$\endgroup$
4
  • $\begingroup$ This has the disadvantage that if you copy the figure for use elsewhere it stores all the data of invisible plots. ;-) $\endgroup$ Jan 14, 2015 at 16:51
  • $\begingroup$ @SjoerdC.deVries You can add /. {___, _@_Opacity, ___} -> {} after the ListPlot :-) $\endgroup$
    – ybeltukov
    Jan 14, 2015 at 17:11
  • $\begingroup$ This is it! Perfect, thank you! Both suggested solutions work. I preferred this one, since it's loading all graphs simultaneously and only toggles their opacity. I'm hoping this way is a bit faster when working with a lot of graphs at the same time. Thanks again to both of you! $\endgroup$
    – Jason
    Jan 14, 2015 at 18:52
  • $\begingroup$ I'd love to vote both of you up, but can't yet due to missing reputation. $\endgroup$
    – Jason
    Jan 14, 2015 at 18:55
3
$\begingroup$
data = {{1, 2, 3}, {2, 5}, {0, 6}};
Manipulate[
 ListPlot[data[[x]], Joined -> True, PlotRange -> {-1, 5}], 
 {{x, {1}}, Range@Length@data, ControlType -> TogglerBar}
]

Mathematica graphics

With more togglers toggled:

Mathematica graphics

With differently size data

data = {{1, 2, 3}, {2, 5}};
Manipulate[
 ListPlot[data[[x]], Joined -> True, PlotRange -> {-1, 5}], 
 {{x, {1}}, Range@Length@data, ControlType -> TogglerBar}
]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Colors depend on the choice of curves... $\endgroup$
    – ybeltukov
    Jan 14, 2015 at 16:24
  • $\begingroup$ @ybeltukov If that would really be a problem one could add PlotStyle -> (ColorData[1, #] &) /@ x or whatever color scheme one woulkd like to use. $\endgroup$ Jan 14, 2015 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.