Wolfram Research

Function Repository Resource:

PrettyGrid

Source Notebook

Conveniently display two-dimensional data in a nicely styled grid

Contributed by: Michael Sollami

ResourceFunction["PrettyGrid"][data]

displays the elements of data in an autostyled two-dimensional grid.

Details and Options

Grid works only on lists of lists, but ResourceFunction["PrettyGrid"] works on data of many forms:
lists { expr 1 , expr 2 , }
associations <| k 1 v 1 , k 2 v 2 , |>
matrices { { expr 11 , expr 12 , } , { expr 21 , expr 22 , } , }
list of rules { k 1 v 1 , k 2 v 2 , }
list of lists of rules { { k 11 v 11 , } , { k 21 v 21 , } , }
list of associations { <| k 11 v 11 , |> , <| k 21 v 21 , |> }
ResourceFunction["PrettyGrid"] takes the following options:
Alignment Left horizontal alignment of items
"BaseFontSize" Default the font size used in BaseStyle of the grid
"ColumnHeadings" Automatic the headers used for the columns
PlotTheme "Monochrome" format the grid with a predefined set of styles
"RowHeadings" Automatic the headers used for the rows
Spacings Automatic this spec is passed directly to Grid
The following option values are available for the PlotTheme:
"Monochrome"
"Minimal"
"Detailed"
"Marketing"

Examples

Basic Examples

PrettyGrid displays matrices with formatting by default:

In[1]:=
ResourceFunction["PrettyGrid"][Table[10 i + j, {i, 5}, {j, 10}], Spacings -> {1, 1}]
Out[1]=

PrettyGrid accepts mixed lists of lists of rules and associations:

In[2]:=
ResourceFunction[
 "PrettyGrid"][{{"Apples" -> 3, "Pears" -> 0, "Oranges" -> 2}, {"Apples" -> 1, "Pears" -> 6, "Oranges" -> 3}, <|
   "Apples" -> 3, "Pears" -> 2, "Oranges" -> 1|>}, PlotTheme -> "Detailed"]
Out[2]=

PrettyGrid works on both rectangular and ragged matrices:

In[3]:=
ResourceFunction["PrettyGrid"][
 SparseArray[{{1, 1} -> 1, {2, -1} -> 2, {3, _} -> 3}, {3, 15}], Spacings -> {.8, .8}]
Out[3]=

PrettyGrid works on certain datasets:

In[4]:=
d = ExampleData[{"Dataset", "Titanic"}];
ResourceFunction["PrettyGrid"][d[[;; 10]], PlotTheme -> "Minimal"]
Out[5]=

PrettyGrid works on ragged arrays:

In[6]:=
ResourceFunction["PrettyGrid"][
 Table[RandomChoice[{RandomReal[], n}], {i, 7}, {j, 7}] /. n -> Nothing]
Out[6]=

PrettyGrid attempts to infer headings from the table's content:

In[7]:=
ResourceFunction[
 "PrettyGrid"]@{<|"Apples" -> 1, "Pears" -> 2, "Oranges" -> 3|>, <|
   "Apples" -> 2, "Oranges" -> 4, "Bananas" -> 2|>, <|"Oranges" -> 2, "Pears" -> 3, "Bananas" -> 9|>}
Out[7]=

Scope

PrettyGrid also works with nested associations:

In[8]:=
ResourceFunction[
 "PrettyGrid"]@<|
  "Monday" -> <|"Apples" -> 1, "Pears" -> 2, "Oranges" -> 3|>, "Tuesday" -> <|"Apples" -> 2, "Oranges" -> 4, "Bananas" -> 2|>, "Wednesday" -> <|"Oranges" -> 2, "Pears" -> 3, "Bananas" -> 9|>|>
Out[8]=

Options

PrettyGrid takes an Alignment option for controlling horizontal item‐wise alignments:

In[9]:=
Manipulate[
 ResourceFunction[
  "PrettyGrid"][{<|"Apples" -> 1, "Pears" -> 2, "Oranges" -> 3|>, <|
    "Apples" -> 2, "Oranges" -> 4, "Bananas" -> 2|>, <|"Oranges" -> 2,
     "Pears" -> 3, "Bananas" -> 9|>},
  Alignment -> a, "BaseFontSize" -> 14], {a, {Left, Center, Right}}]
Out[9]=

PrettyGrid takes an optional list of "ColumnHeadings" and "RowHeadings":

In[10]:=
d = Table[RandomInteger[100], {i, 5}, {j, 3}];
ResourceFunction["PrettyGrid"][d, "ColumnHeadings" -> {"A", "B", "C"},
  "RowHeadings" -> {"1", "2", "3", "4"}, "BaseFontSize" -> 14]
Out[11]=

PrettyGrid has the Spacings option:

In[12]:=
d = RandomInteger[100, {5, 3}];
Manipulate[
 ResourceFunction["PrettyGrid"][d\[Transpose], "RowHeadings" -> {"A", "B", "C"}, Spacings -> {xs, ys}] , {{xs, 2}, 0, 4}, {{ys, 1}, 0, 4}]
Out[12]=

Compare the four available settings for the PlotTheme option:

In[13]:=
r = RandomInteger[{1, 100}, {5, 12}]; Manipulate[
 ResourceFunction["PrettyGrid"][r, PlotTheme -> plotTheme], {plotTheme, {"Detailed", "Marketing", "Minimal", "Monochrome" }}]
Out[13]=

Properties and Relations

For small 2D data, Dataset does similar things, but sometimes does not format as expected:

In[14]:=
d = {{"Apples" -> 1, "Grapes" -> 2, "Oranges" -> 3}, <|"Apples" -> 3, "Pears" -> 2, "Oranges" -> 1|>};
Row@{ResourceFunction["PrettyGrid"]@d, Spacer[20], Dataset@d}
Out[12]=

Resource History

License Information