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{expr1,expr2,}
associations<|k1v1,k2v2,|>
matrices{{expr11,expr12,},{expr21,expr22,},}
list of rules{k1v1,k2v2,}
list of lists of rules{{k11v11,},{k21v21,},}
list of associations{<|k11v11,|>,<|k21v21,|>}
ResourceFunction["PrettyGrid"] takes the following options:
AlignmentLefthorizontal alignment of items
"BaseFontSize"Defaultthe font size used in BaseStyle of the grid
"ColumnHeadings"Automaticthe headers used for the columns
PlotTheme"Monochrome"format the grid with a predefined set of styles
"RowHeadings"Automaticthe headers used for the rows
SpacingsAutomaticthis spec is passed directly to Grid
The following option values are available for the PlotTheme:
"Monochrome"
"Minimal"
"Detailed"
"Marketing"

Examples

Basic Examples (6) 

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 (1) 

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 (4) 

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 (1) 

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]=

Publisher

Michael Sollami

Version History

  • 1.0.1 – 23 November 2020
  • 1.0.0 – 04 March 2020

License Information