Message Boards Message Boards

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

Posted 9 years ago

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]
POSTED BY: C P
8 Replies
Posted 9 years ago

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.

POSTED BY: C P
Posted 9 years ago

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.

POSTED BY: Elton TeKolste
Posted 9 years ago

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]

POSTED BY: Elton TeKolste

Hi CP,

Can you support with some (experimental) data?

POSTED BY: Jos Klaps
Posted 9 years ago

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.

POSTED BY: C P

Hi CP,

Sorry, I cant see what is wrong.

Please can someone help? Thanks........Jos

POSTED BY: Jos Klaps
Posted 9 years ago

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): enter image description here

If I select "p4", the curve is supposed to be red, but it's blue! enter image description here

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.

POSTED BY: C P
Posted 9 years ago

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

POSTED BY: Elton TeKolste
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract