0
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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

$\endgroup$
  • 2
    $\begingroup$ 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? $\endgroup$ – Manfredo Nov 27 '17 at 15:06
  • $\begingroup$ @Manfredo check this mathematica.stackexchange.com/questions/1731/… $\endgroup$ – zhk Nov 27 '17 at 16:53

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.