2
$\begingroup$

I have the following Table:

{{X1,X2,0.34,0.31,0.45,0.62,0.26,0.91,0.16},{X3,X4,0.67,0.21,0.11,0.62,0.46,0.51,0.76},{X5,X6,0.04,0.11,0.85,0.92,0.06,0.71,0.26},{X7,X8,0.74,0.11,0.15,0.89,0.11,0.99,0.00},{X9,X10,0.44,0.27,0.25,0.42,0.56,0.21,0.76},{X11,X12,0.78,0.11,0.15,0.72,0.27,0.61,0.36}}

How can I build a color code on the background of the numbers depending on their value? For instance, if v=value:

If[v>65,background[v]="green"

If[45<v<64,background[v]="yellow"

If[v<44,background[v]="red"

If possible, with many color gradations also, in order to have several ranges.

Moreover, how can I create an other table by selecting just the rows which the value - for instance - of the third column is upper than 0.85 and/or the value of the second to last is more than 90?

$\endgroup$

1 Answer 1

3
$\begingroup$

Update 2 Add header row

headers = {"home", "away", "1[%]", "X[%]", "2[%]", "G[%]", "NG[%]", "o2.5[%]", ""};
withHeader = m // Prepend[headers]
colors = Table[{i, j} -> Which[
       ! NumberQ@withHeader[[i, j]], White,
       True, Blend[{Red, Yellow, Green}, withHeader[[i, j]]]], 
     {i, 7}, {j, 9}] // Flatten;

Grid[withHeader, Background -> {None, None, colors}, Frame -> All]

enter image description here

Update 1

Use blended colors

colors = Table[{i, j} -> Which[
      ! NumberQ@m[[i, j]], White,
      True, Blend[{Red, Yellow, Green}, m[[i, j]]]],
    {i, 6}, {j, 9}] // Flatten;

enter image description here

Here is one way

m = 
  {{X1, X2, 0.34, 0.31, 0.45, 0.62, 0.26, 0.91, 0.16},
   {X3, X4, 0.67, 0.21, 0.11, 0.62, 0.46, 0.51, 0.76},
   {X5, X6, 0.04, 0.11, 0.85, 0.92, 0.06, 0.71, 0.26},
   {X7, X8, 0.74, 0.11, 0.15, 0.89, 0.11, 0.99, 0.00},
   {X9, X10, 0.44, 0.27, 0.25, 0.42, 0.56, 0.21, 0.76},
   {X11, X12, 0.78, 0.11, 0.15, 0.72, 0.27, 0.61, 0.36}};

colors = Table[{i, j} -> Which[
      ! NumberQ@m[[i, j]], White,
      m[[i, j]] > .65, Green,
      .45 < m[[i, j]] && m[[i, j]] < .65, Yellow, 
      m[[i, j]] < .44, Red,
      True, White],
    {i, 6}, {j, 9}] // Flatten;

Grid[m, Background -> {None, None, colors}, Frame -> All]

enter image description here

$\endgroup$
7
  • $\begingroup$ Amazing Rohit, it is exactly what I was looking for, really thank you! $\endgroup$
    – Nate
    Feb 12, 2020 at 21:14
  • 1
    $\begingroup$ You are welcome. Would you mind upvoting and accepting the answer? $\endgroup$ Feb 12, 2020 at 21:43
  • $\begingroup$ Done! Rohit I tried it on my code but it doesn't work. This is what I've done more: headers = {"home", "away", "1[%]", "X[%]", "2[%]", "G[%]", "NG[%]", "o2.5[%]"}; t=TableForm[SetPrecision[m, 2], TableHeadings -> {None, headers}] then I added your code: colors = Table[{i, j} ->Which[! NumberQ@tab[[i, j]], White, True,Blend[{Red, Yellow, Green}, t[[i, j]]]], {i,Length[matches]}, {j, Length[tab[[1]]]}] // Flatten where am I wrong? $\endgroup$
    – Nate
    Feb 13, 2020 at 17:58
  • $\begingroup$ TableForm does not support background colors, have to use Grid. $\endgroup$ Feb 13, 2020 at 20:08
  • 1
    $\begingroup$ Answer updated to include header row. $\endgroup$ Feb 13, 2020 at 20:48

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.