Multiple background subregions in the Mathematica grid

I cannot figure out how to identify several subregions with a different color, as shown below.

Any idea?

Many thanks,

LA

Grid[Table["g", {4}, {7}],
Background -> {None, None, {{{1, 3}, {1, 3}} -> LightRed}}]
+5
source share
2 answers

Just specify the regions and colors, as you already have the first:

Grid[Table["g", {4}, {7}], 
 Background -> {None, None, {
    {{1, 3}, {1, 3}} -> LightRed,
    {{3, 4}, {4, 7}} -> LightBlue
  } } ]

enter image description here

+6
source

As a note, it is usually required to specify background colors depending on the values.

To do this, you can:

k = Table[RandomInteger[{1, 2}], {4}, {7}];
Grid[k,
 Background ->
  {None, None,
   Join[
    Position[k, 1] /. {x_, y_} -> ({x, y} -> LightRed), 
    Position[k, 2] /. {x_, y_} -> ({x, y} -> LightBlue)]
   }]

enter image description here

Edit

If you do not know a priori the range of values, you can try something like:

k = Table[RandomInteger[{1, 20}], {4}, {7}];
Grid[k,
 Frame -> All,
 ItemStyle -> Directive[FontSize -> 16], 
 Background ->
  {None, None, 
   Flatten@Array[List[##] ->
                   ColorData["Rainbow"][(k[[##]] - Min@k) / Max@k] &, 
           Dimensions@k]
  }  
]

enter image description here

+7
source

All Articles