Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list of lists looking like this: {{{x1, y1}, {x2, y2}}, {x1, y1},...}}. Furthermore I have defined a legend, which is taken from the filenames. Now I want to plot this data dynamically, add/remove sub-lists in a ListLinePlot, e.g. with a Checkbox. This is the part of the code I have so far:

Importing the data:

files = FileNames["*.dat"];
data = Import[#, "Data"] & /@ files;
legend = StringReplace[files, ".dat" -> ""];

So far I have made the plot like this:

plot1 = ListLinePlot[data[[1]], PlotLegends -> legend[[1]]];
plot2 = ListLinePlot[data[[2]], PlotLegends -> legend[[2]]];

Manipulate[
Show[
  If[x1 == True, plot1, Sequence @@ {}],
  If[x2 == True, plot2, Sequence @@ {}],
  {{x1, True, "plot1"}, {True, False}}, {{x2, True, "plot2"}]

Is there maybe a more elegent way of realizing this, maybe automatically, for list of sub-lists?

share|improve this question
    
Your code appears incomplete or damaged. Please correct it. –  Mr.Wizard Sep 2 at 9:52
    
I hope now its ok. –  andeee Sep 2 at 9:57
1  
related How to dynamically toggle curves on/off ...? –  kguler Sep 2 at 10:29

3 Answers 3

up vote 4 down vote accepted

Here's a version with checkboxes:

numData = 4;
data    = Sort /@ RandomReal[{0, 10}, {5, numData, 2}];
legend  = Table["data" <> ToString@i, {i, numData}];

DynamicModule[
  { dataCombinations = {} }
  ,
  Grid @ {{
    CheckboxBar[
      Dynamic @ dataCombinations, 
      Thread[Range[numData] -> legend], 
      Appearance -> "Vertical"
    ],
    Dynamic @ ListPlot[
      data[[dataCombinations]], 
      Joined -> True, 
      PlotLegends -> legend[[dataCombinations]]
    ]
 }}
]

Mathematica graphics

share|improve this answer

Using some made-up test data to illustrate. This approach is also used here with fading transitions.

data1 = Table[Sin[x], {x, 0, 2 Pi, Pi/4}];
data2 = Table[Cos[x], {x, 0, 2 Pi, Pi/4}];

plot1 = ListLinePlot[data1, DataRange -> {0, 2 Pi}];
plot2 = ListLinePlot[data2, DataRange -> {0, 2 Pi}];

id = ImageDimensions[plot1];

Manipulate[Graphics[{White, Rectangle[{0, 0}, id],
   Switch[chart,
    "   Plot 1   ", Inset[plot1, Center, Center, id],
    "   Plot 2   ", Inset[plot2, Center, Center, id],
    " Plot 1 & 2 ", Inset[Show[plot1, plot2], Center, Center, id]]},
  PlotRange -> {{0, First[id]}, {0, Last[id]}}, ImageSize -> id],
 {{chart, "   Plot 1   ", "Displayed: "},
  {"   Plot 1   ", "   Plot 2   ", " Plot 1 & 2 "}},
 ControlType -> SetterBar, SaveDefinitions -> True]

enter image description here

share|improve this answer
    
Thank you for your answer, but how can i display both Plots? I would like to compare different plots interactively but can not manage it. –  andeee Sep 2 at 9:56
    
@andeee - See edit. –  Chris Degnen Sep 2 at 10:03

Consider:

plot1 = Plot[Sin[x], {x, 0, 6 Pi}];
plot2 = Plot[Cos[x], {x, 0, 6 Pi}];

TogglerBar[Dynamic[p], {plot1 -> "Plot 1", plot2 -> "Plot 2"}]

Dynamic[p /. {x__} :> Show[x]]

See TogglerBar and CheckboxBar.

share|improve this answer

Your Answer

 
discard

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.