4
$\begingroup$

I want to use Grid to tabulate the following list and have the same background color for rows of the same value on column 4. The actual list is very long.

list=
{{1, {2, 1, 1}, {1, 0, -1, 1}, 4.48},
 {2, {-2, -1, -1}, {-1, 0, 1, -1}, 4.48},
 {3, {2, 1, 1}, {1, 0, -1, 1}, 4.48},
 {4, {-2, -1, -1}, {-1, 0, 1, -1}, 4.48},
 {9, {-2, -1, 1}, {-1, 0, 1, 1}, 4.48},
 {10, {2, 1, -1}, {1, 0, -1, -1}, 4.48},
 {11, {2, 1, -1}, {1, 0, -1, -1}, 4.48},
 {12, {-2, -1, 1}, {-1, 0, 1, 1}, 4.48},
 {5, {0, -1, 0}, {1, -2, 1, 0}, 5.266},
 {6, {0, -1, 0}, {1, -2, 1, 0}, 5.266},
 {7, {0, 1, 0}, {-1, 2, -1, 0}, 5.266},
 {8, {0, 1, 0}, {-1, 2, -1, 0}, 5.266},
 {5, {1, -4, 0}, {2, -3, 1, 0}, 5.6323},
 {6, {1, -4, 0}, {2, -3, 1, 0}, 5.6323},
 {7, {-1, 4, 0}, {-2, 3, -1, 0}, 5.6323},
 {8, {-1, 4, 0}, {-2, 3, -1, 0}, 5.6323}}

I found a solution to a similar question here. I wonder how can I build the colorrules elegantly. Thanks.

$\endgroup$

4 Answers 4

5
$\begingroup$

Here is a solution with Item[...] :

itemRules = 
  With[{color = RandomColor[]},
    (x : {___, #}) :>  (Item[#,Background -> color] & /@ x)] & /@ Union[list[[All, 4]]];

Grid[list /. itemRules, Dividers -> All] 

enter image description here

$\endgroup$
4
$\begingroup$
sets = GatherBy[Transpose[{Range[Length[list]], list[[All, 4]]}], #[[2]] &];
colors = {LightYellow, LightOrange, LightBlue};
colorrules = 
 Flatten@Table[
   Thread[sets[[n, All, 1]] -> colors[[n]]], {n, Length[sets]}];

Grid[list, Frame -> All, Background -> {None, colorrules}]

enter image description here

$\endgroup$
4
$\begingroup$

Using Association and Lookup to specify row backgrounds:

keys = Union @ list[[All, -1]];
colors = Lighter @* Lighter @* ColorData["Rainbow"] /@ Rescale[keys];
backgroundColors = AssociationThread[keys, colors];

Legended[Grid[list,  Frame -> All, 
  Background -> {None, Lookup[list[[All, -1]]] @ backgroundColors}],
 SwatchLegend[colors, keys]]

enter image description here

$\endgroup$
0
$\begingroup$

The actual list is very long.

It is likely that there are more than three categories and the data is random. Using built-in ColorData functionality with random data, the result will be presented with unsorted and sorted data (based on the last column):

Clear["Global`*"];
SeedRandom[1];
(data = {#, Sequence @@ RandomInteger[{-4, 4}, {1, 3}], 
      Sequence @@ RandomInteger[{-1, 1}, {1, 4}], 
      RandomReal[{-2.5, 5.5}]} & /@ Range[12]) ;

alpine = Plot3D[PDF[BinormalDistribution[1/3]
    , {x, y}], {x, -3, 3}, {y, -3, 3}
   , ColorFunction -> "AlpineColors"
   , SphericalRegion -> True
   , ImageSize -> 300
   , PlotLabel -> Style["AlpineColors", Black, Bold, 20]
   ];

{min, max} = data[[All, -1]] // MinMax;
cols = ColorData["AlpineColors"] /@ 
  Rescale[Last /@ ReverseSortBy[data, Last]];
lg = BarLegend[{"AlpineColors", {min, max}}];
g1 = Labeled[Grid[ReverseSortBy[data, Last]
    , Frame -> All
    , Background -> {None, cols}
    ]
   , lg, Right
   ];

colsraw = ColorData["AlpineColors"] /@ Rescale[Last /@ data];
g2 = Labeled[Grid[data
    , Frame -> All
    , Background -> {None, colsraw}
    ]
   , lg, Right
   ];

Multicolumn[{g1, g2, alpine}, 3]

AlpineColors for grid rows

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.