Plotting more curves with "switchable" legend - no color correspondence...

Hello everybody,
I want to plot many series of data on the same graph and I am using a as legend a Checkboxbar to be able to switch some data series on and off on the graph. The problem is that the colors in Legend are not corresponding to the ones from graph (curves)! It seems that each time I switch on additional data series, the new curves are getting randomly new colors, while the colors in Legend are staying the same (the “Legend” is shown permanently, regardless the on or off state of a data set). Is there a way to “connect” the curves displayed to the colors from Legend?

Manipulate[
 ListPlot[projects[[project]], Joined -> True, 
  PlotRange -> {{-80, 0}, {0, 80}}, 
  GridLines -> {{-10, -20, -30, -40, -50, -60, -70}, {10, 20, 30, 40, 
     50, 60, 70}}, AxesLabel -> {x, z}, PlotLabel -> "My Data Sets", 
  ImageSize -> Large], {{project, {1}}, 
  Thread[Range[Length@projects] -> 
    MapThread[
     Row[{Pane@
         Graphics[{#1, Rectangle[{0, 0}, {10, 1}]}, ImageSize -> 50], 
        "   " <> prjnames[[#2]]}, 
       BaselinePosition -> Center] &, {ColorData[97] /@ Range[8], 
      Range[8]}]], ControlType -> CheckboxBar, 
  BaselinePosition -> Center, Method -> "Active", 
  Appearance -> "Vertical"}, ControlPlacement -> Right]

Hi CP,

Can you support with some (experimental) data?

Yes, for example:

ClearAll;

projects = RandomReal[#, 100] & /@ {1, 2, 3, 4, 5, 6, 7, 8};

prjnames = {"p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8"};

Manipulate[
 ListPlot[projects[[project]], Joined -> True, AxesLabel -> {x, z}, 
  PlotLabel -> "My Data Sets", ImageSize -> Large], {{project, {1}}, 
  Thread[Range[Length@projects] -> 
    MapThread[
     Row[{Pane@
         Graphics[{#1, Rectangle[{0, 0}, {10, 1}]}, ImageSize -> 50], 
        "   " <> prjnames[[#2]]}, 
       BaselinePosition -> Center] &, {ColorData[97] /@ Range[8], 
      Range[8]}]], ControlType -> CheckboxBar, 
  BaselinePosition -> Center, Method -> "Active", 
  Appearance -> "Vertical"}, ControlPlacement -> Right]

P.S.: I’m using Mathematica 10.02.

Hi CP,

Sorry, I cant see what is wrong.

Please can someone help? Thanks…Jos

Hello Jos Klaps,

I’ll try to explain it some other way:If no dataset is selected, it looks like this (please observe that the colors in legend are pre-defined):

If I select “p4”, the curve is supposed to be red, but it’s blue!

In fact, the first activated dataset get allways the blue color and if I activate more datasets, they will get random colors, not according to the ones in the legend!
My target is, of course, to have a correspondence between the curves colors and the legend colors.
I hope I was more clear this time.
Thanks in advance for every advice.

You need to specify the correct color for each plot separately and combine them using “Show.”

ClearAll;

projects = RandomReal[#, 100] & /@ {1, 2, 3, 4, 5, 6, 7, 8};

prjnames = {“p1”, “p2”, “p3”, “p4”, “p5”, “p6”, “p7”, “p8”};

Manipulate[
Show[ListPlot[projects[[#]], Joined → True, AxesLabel → {x, z},
PlotLabel → “My Data Sets”, ImageSize → Large,
ColorFunction → Function[x, ColorData[5][#]]] & /@
project], {{project, {1}},
Thread[Range[Length@projects] →
MapThread[
Row[{Pane@
Graphics[{#1, Rectangle[{0, 0}, {10, 1}]}, ImageSize → 50],
" " <> prjnames[[#2]]},
BaselinePosition → Center] &, {ColorData[5] /@ Range[8],
Range[8]}]], ControlType → CheckboxBar,
BaselinePosition → Center, Method → “Active”,
Appearance → “Vertical”}, ControlPlacement → Right]

Oh: I do not seem to have “ColorData[97]” so I substituted “ColorData[5].”

Thanks a lot Elton, this solves my problem!
I am still a beginner in Mathematica and I have difficulties to learn the syntax… due to a lot of “shortcuts” like #, &, /.,/@, etc., the almost infinite embedding possibilities and, in many cases, the not documented “combination” of functions and options…
That’s why is a bit discouraging to see how “easy” the solution for some syntax problem is and realizing that I would have never found it out by myself…
Thanks again.

I am all too familiar with the frustrations of the Mathematica syntax – and expect to experience them often in the future.

In particular, I had to analyze your control structure to understand it. It is a clever idea for a pleasant interface.