I'm trying to format a raster-style table with row and column headings and in which each cell is shaded according to the value in the cell, and the value of the cell is displayed in as text in a contrasting shade, for example
I can kludge something together that works:
drawTable[s_] := With[
{
probs = Reverse[Table[RandomReal[], {Length[s]}, {Length[s]}]]
},
Show[Graphics[
Raster[
Append[
Transpose[Prepend[Transpose[1 - probs], Table[1, {Length[s]}]]],
Table[1, {Length[s] + 1}]]
]
],
Epilog -> {
Table[
Text[Style[s[[i]], Bold, GrayLevel[0.2]], {0.5,
Length[s] - i + 0.5}], {i, Length[s]}],
Table[
Text[Style[s[[i]], Bold, GrayLevel[0.2]], {i + 0.5,
Length[s] + 0.4}], {i, Length[s]}],
Table[
{GrayLevel[If[# <= 0.5, 0, 1] &[probs[[i, j]]]], Text[
If[# == 0 , "", #] &[Round[#, N[10^-3]] ] &[probs[[i, j]]],
{.5 + j, -.5 + i}]
},
{i, Length[s]}, {j, Length[s]}]
}
]
]
drawTable[{"A", "B", "C", "D", "E", "F", "H"}]
But there has to be an easier way. I've arrived at positioning by quite a bit of trial and error, resulting in a quite a bit of appending, transposing and un-transposing, and tweaking of positions, and even then the result isn't perfect (for example there is extra blank space above and to the left).
Is there a better way to go about this, either strategically (what a different API) or tactically (being smarter about list handling, especially)?
FWIW, in the actual scenario, I need more control over the formatting of the digits and of the text used for the headings, so I expect Text
and Style
are needed.