# Color code and string choice on a table

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?

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

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


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;


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]


• Amazing Rohit, it is exactly what I was looking for, really thank you! – Nate Feb 12 '20 at 21:14
• You are welcome. Would you mind upvoting and accepting the answer? – Rohit Namjoshi Feb 12 '20 at 21:43
• 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? – Nate Feb 13 '20 at 17:58
• TableForm does not support background colors, have to use Grid. – Rohit Namjoshi Feb 13 '20 at 20:08
• Answer updated to include header row. – Rohit Namjoshi Feb 13 '20 at 20:48