1
$\begingroup$

Given some number between 0 and 1:

RandomReal[1, 100]

I'm trying to color the values based on their size. I tried it like this:

numbersColored = 
 Table[Style[numbers[[x]], Darker[Red, 1 - numbers[[x]]]], {x, 1, 
   Length@numbers}]

which uses red and Darker. I was wondering, however, if there is a more elegant way (without using Table for example) of doing this that also lets me choose some color function?

$\endgroup$
  • $\begingroup$ This isn't really any different than your code, just using Map instead of Table: style = Style[#, Darker[Red, 1 - #]] &; style /@ numbers $\endgroup$ – Jason B. Aug 16 '17 at 17:47
  • 1
    $\begingroup$ In my opinion, a more readable Table version would be Table[Style[n, Darker[Red, 1-n]], {n, numbers}] $\endgroup$ – Carl Woll Aug 16 '17 at 17:54
  • $\begingroup$ Jason B.'s (pruned) is elegant too: Style[#,Darker[Red,1-#]]& /@ numbers (and doesn't use Table) $\endgroup$ – David G. Stork Aug 16 '17 at 17:57
  • $\begingroup$ Thanks for these solutions! But how about using some sort of color function instead of one single color? I wasn't able to figure this out yet ;) $\endgroup$ – holistic Aug 16 '17 at 18:00
  • 3
    $\begingroup$ @holistic you can use any color function, like Style[#, Hue[#]] & /@ numbers or Style[#, ColorData["Rainbow"][#]] & /@ numbers $\endgroup$ – Jason B. Aug 16 '17 at 18:05
3
$\begingroup$
Style[#, ColorData[3, "ColorList"][[Round[10 #]]]] & /@ numbers
$\endgroup$
  • $\begingroup$ Thank you David! $\endgroup$ – holistic Aug 16 '17 at 19:23

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.