7
$\begingroup$

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.

$\endgroup$
7
  • 1
    $\begingroup$ please use a meaningful example instead of dummy names $\endgroup$
    – rm -rf
    Mar 25, 2013 at 15:47
  • 1
    $\begingroup$ @rm-rf Like WHO -> "is in the first base"? $\endgroup$ Mar 25, 2013 at 15:49
  • $\begingroup$ 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. $\endgroup$ Mar 26, 2013 at 9:28
  • $\begingroup$ @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? $\endgroup$
    – Sulli
    Mar 26, 2013 at 10:15
  • 2
    $\begingroup$ 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. $\endgroup$
    – Jagra
    Mar 26, 2013 at 12:34

4 Answers 4

7
$\begingroup$

A slightly different approach with Checkboxes:

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

enter image description here

$\endgroup$
8
  • $\begingroup$ 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. $\endgroup$
    – Federico
    Mar 25, 2013 at 17:25
  • $\begingroup$ @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? $\endgroup$
    – Jagra
    Mar 25, 2013 at 19:19
  • $\begingroup$ I use version 8. Here is a screenshot. The problem is that in version 8 ListPlot[{_, List, _}] is not allowed. $\endgroup$
    – Federico
    Mar 25, 2013 at 20:32
  • $\begingroup$ 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. $\endgroup$
    – murray
    Mar 25, 2013 at 21:57
  • $\begingroup$ @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? $\endgroup$
    – Sulli
    Mar 26, 2013 at 10:06
8
$\begingroup$

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]}],
        ListPlot[{}]];

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]

CheckboxLineLegend

but ListPicker and TogglerBar give good alternatives, too:

ListPickerLineLegend

TogglerLineLegend


Update: Dimming the inactive line legend items

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

Manipulate[
 Show[plots[[datasetNo /. {} -> 4]], PlotRange -> All],
 Row[{
  Control[{{datasetNo, {1, 3}, ""}, {1 -> "", 2 -> "", 3 -> ""}, 
   ControlType -> CheckboxBar, BaselinePosition -> Center, 
   Method -> "Active", Appearance -> "Vertical"}], 
  Dynamic@Column[
   MapThread[
    Row[{Pane@
      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]

dimmedInactiveLineLegend

$\endgroup$
2
  • $\begingroup$ Version 9 user can use ListPlot[{""}] to generate the empty plot and ColorData[1] to get the default colors. $\endgroup$
    – Karsten7
    Jun 18, 2015 at 8:18
  • 1
    $\begingroup$ Clickable line legends! Great solution +1 from me. $\endgroup$
    – Jagra
    Jun 19, 2015 at 12:21
3
$\begingroup$

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

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

and then you could do:

Manipulate[
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":

Manipulate[
 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];
Manipulate[
 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}]
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Federico
    Mar 25, 2013 at 16:28
  • $\begingroup$ @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. $\endgroup$ Mar 25, 2013 at 16:47
1
$\begingroup$
data = RandomReal[{#, # + 1}, 24] & /@ Range[5];
Manipulate[
 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}]
$\endgroup$

Your Answer

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

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