4
$\begingroup$

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?

$\endgroup$
5
$\begingroup$

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

| improve this answer | |
$\endgroup$
4
$\begingroup$

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – andeee Sep 2 '14 at 9:56
  • $\begingroup$ @andeee - See edit. $\endgroup$ – Chris Degnen Sep 2 '14 at 10:03
4
$\begingroup$

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.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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