Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Manipulate to visualize three datasets:

Manipulate[ListPlot[WHAT -> what], {what, {dataset1, dataset2, dataset3}}]

Now with this solution I can only plot data for either dataset1, dataset2 or dataset3.

What if I wanted to plot the data for BOTH dataset1 and dataset2 on the same graph? Or for all three datasets? An easy way such as checking boxes corresponding to the datasets would be nice, but I don't know how to integrate this with Manipulate.

share|improve this question
please use a meaningful example instead of dummy names – R. M. Mar 25 '13 at 15:47
@rm-rf Like WHO -> "is in the first base"? – Dr. belisarius Mar 25 '13 at 15:49
I would say that the title is misleading. I understood it in the way that one wants to pick up several numeric values from a list shown in Manipulate. – Alexei Boulbitch Mar 26 '13 at 9:28
@AlexeiBoulbitch that's what I wanted to do in the first place (see my comment below to Jagra's answer), and the question was clear, but then rm-rf told me to delete dummy names which led to more confusion than originally. Dummy names were there for a good reason, my problem comes from the fact that I work on custom functions, if dummy names are "transparent" (eg "myList") I don't see why we should avoid them? – Sulli Mar 26 '13 at 10:15
At this point, I don't have a clue about what you really want Clarifying a question helps everyone. Changing what you want from a question disregards the efforts that people make in producing answers. Two seeming good answers have come for this question. I'd recommend accepting the best of them (mine! mine! Mine! ;-) then giving some more thought to formulating and submitting a second question. – Jagra Mar 26 '13 at 12:34
up vote 6 down vote accepted

A slightly different approach with Checkboxes:

  dataset = RandomReal[#, 100] & /@ {1, 2, 3};
  ListPlot[dataset[[{x, y, z}]], Joined -> True]
  ], {x, {0, 1}, Checkbox}, {y, {0, 2}, Checkbox}, {z, {0, 3}, 

enter image description here

share|improve this answer
It doesn't work for me. When x, y, or z are unchecked, they are 0 and dataset[[0]] is List, so ListPlot is passed something like {List, {0.12, ...}, List}, which doesn't work. – Federico Mar 25 '13 at 17:25
@Federico -- Odd. Works perfectly for me with any and all combinations of the different checkboxes. I run version 9. Under what version have you tried this? – Jagra Mar 25 '13 at 19:19
I use version 8. Here is a screenshot. The problem is that in version 8 ListPlot[{_, List, _}] is not allowed. – Federico Mar 25 '13 at 20:32
Works OK here with Mathematica 9.0.1 (under OS X 10.8.3) with all three unchecked as well as all 1-box, 2-box, and 3-box combinations tried, too. – murray Mar 25 '13 at 21:57
@Federico Thanks it works, but because of rm-rf who told me to rephrase my question, actually what I want to do is a little bit different. Let's imagine I have a custom function like: Manipulate[ customFunction[{"myList"}, OPTION -> option], {option, listOfOptions, ControlType -> "SetterBar"}] , but instead of a SetterBar, I'd like a TogglerBar (allowing me to select multiple values at the same time from listOfOptions ). How would I do that? – Sulli Mar 26 '13 at 10:06

With the arbitrary datasets

datasets = {dataset1, dataset2, dataset3} = RandomReal[#, 100] & /@ {1, 2, 3};

one can pre-render the plots and add an empty plot for the case when no dataset is selected

plots = Append[
         MapThread[ListPlot[#1, Joined -> True, PlotStyle -> #2] &, 
          {datasets, ColorData[97] /@ Range[3]}],

A relative intuitive way to interactively select the datasets to be plotted within Manipulate can be achieved by creating a clickable line legend. Different control types can be used to achieve this. In my opinion the most unambiguous one is generated using checkboxes

Manipulate[Show[plots[[datasetNo /. {} -> 4]], PlotRange -> All],
 {{datasetNo, {1}, ""}, 
  Thread[Range[3] -> MapThread[
   Row[{Pane@Graphics[{#1, Rectangle[{0, 0}, {10, 1}]}, ImageSize -> 20], 
    "  Dataset " <> ToString@#2}, BaselinePosition -> Center] &,                     
   {ColorData[97] /@ Range[3], Range[3]}]], 
  ControlType -> CheckboxBar, BaselinePosition -> Center, Method -> "Active", 
  Appearance -> "Vertical"}, ControlPlacement -> Right]


but ListPicker and TogglerBar give good alternatives, too:



Update: Dimming the inactive line legend items

datasetNames = Array["  Dataset " <> ToString@# &, 3]

 Show[plots[[datasetNo /. {} -> 4]], PlotRange -> All],
  Control[{{datasetNo, {1, 3}, ""}, {1 -> "", 2 -> "", 3 -> ""}, 
   ControlType -> CheckboxBar, BaselinePosition -> Center, 
   Method -> "Active", Appearance -> "Vertical"}], 
      Graphics[{#1, Rectangle[{0, 0}, {10, 1}]}, ImageSize -> 20],
      MapAt[Style[#, Opacity[0.35]] &, datasetNames, 
       List /@ Complement[Range[3], datasetNo]][[#2]]}, 
       BaselinePosition -> Center] &, 
    {MapAt[Opacity[0.1, #] &, ColorData[97] /@ Range[3], 
     List /@ Complement[Range[3], datasetNo]], Range[3]}], 
   Left, 0.9, ItemSize -> {Automatic, 0}, BaselinePosition -> Center]}], 
 ControlPlacement -> Right]


share|improve this answer
Version 9 user can use ListPlot[{""}] to generate the empty plot and ColorData[1] to get the default colors. – Karsten 7. Jun 18 '15 at 8:18
Clickable line legends! Great solution +1 from me. – Jagra Jun 19 '15 at 12:21

here's a guess, is that what you want? (too long for a comment)

data = RandomInteger[10, {3, 10}]

and then you could do:

ListPlot[what, Joined -> True], {{what, data}, 
MapIndexed[# -> First[#2] &, data], ControlType -> TogglerBar}]

to get:

enter image description here

EDIT (since I still do not know what the OP seeks, I'll keep above)

Based on Federico's comments (thanks!), here's an adjusted version that doesn't break on empty selection and has dedicated colors per "dataset":

 If[what != {},ListPlot[what, Joined -> True, 
 PlotStyle ->Extract[{Red, Blue, Green}, Position[data, #] & /@ what]], 
 {{what, data}, MapIndexed[# -> First[#2] &, data], 
 ControlType -> TogglerBar}]

Alternatively, to maintain the default colour scheme:

data = RandomReal[{#, # + 1}, 24] & /@ Range[5];
 If[what =!= {},
  ListLinePlot[data[[what]], PlotStyle -> (ColorData[1] /@ what), ImageSize -> {400, 300}],
  Panel[Style["Select some data!", 18, "TI"],
   Alignment -> Center, Background -> White, Appearance -> None, ImageSize -> {400, 300}]],
 {{what, {1}}, Range@Length[data], ControlType -> TogglerBar}]
share|improve this answer
Although very nice, clean and concise, there are two issues: 1) an error is displayed if no dataset is selected; 2) plot lines are subject to colour change (try switching everything off, then click 1, 2, 1). Both of them can be easily fixed. – Federico Mar 25 '13 at 16:28
@Federico: thanks for the input! I wasn't aware that TogglerBar appends the most recently selected choice at the end. As you say "easily fixed", feel free to edit my answer incase you have a better approach - I am sure I must've missed something obvious. – Pinguin Dirk Mar 25 '13 at 16:47
data = RandomReal[{#, # + 1}, 24] & /@ Range[5];
 If[what =!= {},
  ListLinePlot[data[[what]], PlotStyle -> (ColorData[1] /@ what), ImageSize -> {400, 300}],
  Panel[Style["Select some data!", 18, "TI"],
   Alignment -> Center, Background -> White, Appearance -> None, ImageSize -> {400, 300}]],
 {{what, {1}}, Range@Length[data], ControlType -> TogglerBar}]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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