0

I am trying to get the correct colors for my curves. I have this code

color = {Red, Green, Blue};
names = {"cat", "dog", "rat"};
Manipulate[
 Legended[
  Plot[Sin[k x], {x, 0, 3 Pi}, PlotStyle -> color[[k]]], 
  Placed[LineLegend[color, names, LegendLayout -> "Row"], 
   Below]], {{k, {1, 3}}, {1, 2, 3}, ControlType -> CheckboxBar}]

Which instead of assigning color[[k]] (red green and blue) to each curve, it assign the same colour to every curve.

enter image description here

I have already asked a similar question and there is a post which does exactly this, though for a ListLinePlot (last image). I've tried so many different things but none really does the trick.

It is kind of important that I do this in the most simple way because this is a MWE part of a bigger script. In particular I would like to keep the flexibility to increase the number of colors/names without having to modify anything inside the Manipulate.

Manfredo
  • 189
  • 9

1 Answers1

4

Using Evaluate@Sin[k x] will handle this issue.

color = {Red, Green, Blue};
names = {"cat", "dog", "rat"};
Manipulate[
 Legended[Plot[Evaluate@Sin[k x], {x, 0, 3 Pi}, 
   PlotStyle -> color[[k]]], 
  Placed[LineLegend[color, names, LegendLayout -> "Row"], 
   Below]], {{k, {1, 3}}, {1, 2, 3}, ControlType -> CheckboxBar}]

enter image description here

zhk
  • 11,939
  • 1
  • 22
  • 38
  • 2
    I can't believe it's as easy as that. I've spent a day trying to solve this. Why do I have to Evaluate it to have the correct colors? – Manfredo Nov 27 '17 at 15:06
  • @Manfredo check this https://mathematica.stackexchange.com/questions/1731/plot-draws-list-of-curves-in-same-color-when-not-using-evaluate – zhk Nov 27 '17 at 16:53