1

I have imported data for several projects {prj1, prj2, ...} from an excel file, in form of {x,y} coordinates of points. I stored these data in variables and I would like to plot them, also having the possibility to "turn on" or "turn off" some of the projects, so to selectively display different projects. I don't seem to get it work and this is what I've done until now:

prj1 = Import[ExcelFile, {"Data", 5, Table[i, {i, 4, 24}], {1, 2}}];

...

Manipulate[ListPlot[{prj1, prj2, prj3}],
  {prj1, {0, 1}, Checkbox},{prj2, {0, 2}, Checkbox},{prj3, {0, 3}, Checkbox}]

I suspect there is a syntax error, but I need your help to find it...

Thanks in advance.

Conrad

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Conrad
  • 723
  • 4
  • 10
  • Basically the same question, but the proposed solutions doesn't work for me... so I'm still looking for answers. – Conrad Jun 17 '15 at 12:37
  • 2
    Conrad, can you clarify in which ways the solutions proposed in the question linked by @Karsten7. do not work for you? As you say as well, your question and that one sound almost identical... – MarcoB Jun 17 '15 at 12:49
  • You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. (You've formatted your code as a quotation.) – Michael E2 Jun 17 '15 at 13:53

1 Answers1

7

The first answer to the possible duplicate doesn't seem to work under version 10.1. Therefore here is some similar code, that does work:

prjs = {prj1, prj2, prj3} = RandomReal[#, 100] & /@ {1, 2, 3};

Manipulate[ListPlot[prjs[[projectNo]], Joined -> True], 
 {{projectNo, {1}}, Range[Length@prjs], ControlType -> CheckboxBar}]

output

Karsten7
  • 27,448
  • 5
  • 73
  • 134
  • this is I think better than the accepted answer to the dup. -- If this question gets closed you should move this over there. – george2079 Jun 17 '15 at 15:04
  • Just by the way, this does throw an error (w/v9) if you un-check all the boxes. Just a minor annoyance. – george2079 Jun 17 '15 at 15:06
  • 1
    To include PlotLegends and keep fixed ordering of plots: Manipulate[ListPlot[prjs[[projektNr // Sort]], Joined -> True, PlotLegends -> (("prj" <> ToString[#]) & /@ (projektNr // Sort))], {{projektNr, {1}}, Range[3], ControlType -> CheckboxBar}]. @george2079 - with v10.1 on my Mac, I do not get an error when all boxes are un-checked -- just an empty plot. – Bob Hanlon Jun 17 '15 at 15:15
  • @george2079 To generate an empty plot in v9 when all boxes are unchecked, one can use something like ListPlot[prjs[[projectNo /. {} -> {0}]], Joined -> True]. – Karsten7 Jun 17 '15 at 15:41
  • @Karsten 7. Yes, this way works for me! Maybe is there a way to put the names of projects near Checkboxes (for example "prj1", prj2", etc), instead of numbers ? – Conrad Jun 18 '15 at 11:54
  • @Conrad See my answer here for nicer checkboxes with more meaningful text next to them. – Karsten7 Jun 18 '15 at 12:09
  • @Conrad Or just replace Range[Length@prjs] with Thread[Range[ Length@prjs] -> (("prj" <> ToString@#) & /@ Range[Length@prjs])]. – Karsten7 Jun 18 '15 at 12:17
  • @Karsten 7. What I want to have is the name of projects near checkboxes, without any numbering. The names "prj1", "prj2", etc. are just as example, the projects in my application are having real names, without numbering. The order of projects is not very important. – Conrad Jun 18 '15 at 12:27
  • @Conrad In that case I assume the real project names are stored in prjRealNames = {"prj1", "prj2", "prj3"} and now replacing Range[Length@prjs] with Thread[Range[Length@prjs] -> prjRealNames] should do the job. – Karsten7 Jun 18 '15 at 12:37
  • @Karsten 7. ...almost there ;-) but now I get the whole data lists instead of project names near checkboxes... – Conrad Jun 18 '15 at 12:44
  • @Conrad The project names should be put in as strings or unavaluated (using HoldForm, Unevaluated, or Defer; check these posts for multiple different ways if you don't like typing the names in as strings). – Karsten7 Jun 18 '15 at 13:05
  • @Karsten 7. Ok, I don't have a problem typing the names in. I let the numbering in place for the checkboxes and I've added PlotLegends to display the legend for active (checked) datas. But now I've encountered a new problem: there is no correspondence between the activated project (checkbox) and the displayed legend! So if I activate only 1 checkbox, the Legend shows "prj1" also if I selected the checkbox "1" or "5" or etc. ... I tried to put "// Sort" after the list of projects, but no difference! – Conrad Jun 18 '15 at 16:45
  • @Conrad Adding the option PlotLegends -> prjRealNames[[projectNo]] to ListPlot should work. – Karsten7 Jun 18 '15 at 18:44
  • @Karsten 7. I like the solution you described – Conrad Jun 19 '15 at 11:09
  • @Karsten 7. Sorry, I didn't knew I cannot edit my comment after more than 5 minutes... So, I like the solution you described there, but how can I use it to write the real project names near the vertical ordered checkboxes ("active" Legend)? As you know, I stored the project names as strings in the prjnames variable. – Conrad Jun 19 '15 at 11:20
  • @Conrad Replace " Dataset " <> ToString@#2 with " " <>prjnames[[#2]] in that code. – Karsten7 Jun 19 '15 at 12:09
  • @Karsten 7. Perfect!!! Why was that so simple and I had no clue about it? I will have to learn the syntax much more in detail and try to understand the logical background... The last issue I have are the legend colors, not corresponding to the plots colors, I'll try to find out why. Thanks a lot again for your help! – Conrad Jun 19 '15 at 12:18