Function Repository Resource:

TreemapPlot

Source Notebook

Plot a nested list of values as a treemap

Contributed by: Michael Sollami

ResourceFunction["TreemapPlot"][list]

generates a treemap plot of the given list.

Details and Options

The input list accepts arbitrarily deeply nested sublists.
ResourceFunction["TreemapPlot"] takes the following options:
"AspectRatio"1the aspect ratio of the resulting plot
"Background"Automaticthe background styling for all nodes
"ColorFunction"Automatica function f to style the subrectangles, which is called as f[path,area,leafQ]
"FrameStyle"Automaticthe edge styling for all nodes, which can be a directive or a function that is given the path of the node
"ImageWidth"500the width of the resulting plot
"LabelPosition"Leftthe position of the labels (Left,Right, or Center)
"MaximumAspectRatio"4the maximum aspect ratio that can be used in the layout algorithm
"Spacing"0.1amount of padding around branches/levels in the tree

Examples

Basic Examples (1) 

Make a few simple treemaps:

In[1]:=
ResourceFunction["TreemapPlot"][#, "Spacing" -> 0, "ImageWidth" -> 60] & /@ {{1}, {1, 2}, {1, 2, 4}, {1, {1, 2}, 4}}
Out[1]=

Scope (3) 

Style a treemap:

In[2]:=
ResourceFunction[
 "TreemapPlot"][{1, {{4, 2, 3}, 2, {{2, {2, 1, {2, 3, {1, 1, 2, 3}, 3}, 3}}, 2, 3}}},
  "Spacing" -> 0, "ImageWidth" -> 250, "FrameStyle" -> EdgeForm[GrayLevel[.2]],
 "ColorFunction" -> (Directive[
     ColorConvert[Hue[(Length@#1 + 1)/8., 0.8], "RGB"]] &),
 "Background" -> FaceForm[Opacity[.5]], "AspectRatio" -> 1.5]
Out[2]=

Play with the overall aspect and spacings at each depth of the tree:

In[3]:=
a = 1; d = 0;
In[4]:=
Slider[Dynamic[a], {.5, 4}]
Out[4]=
In[5]:=
Slider[Dynamic[d], {0, .6}]
Out[5]=
In[6]:=
Dynamic@ResourceFunction["TreemapPlot"][{1, {1, 1, {1, 1}}}, "Spacing" -> d, "AspectRatio" -> a, "ImageWidth" -> 200, "Background" -> LightBlue]
Out[6]=

Make a tree map with a specific color gradient:

In[7]:=
tree = {{{5, {3, 1}}, {2, {2, 1, {{{5, {3, 1}}, {{{5, {3, 1}}, {{{{5, {3, 1}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 3, 1}}}}, {3, 4, 6, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 1}}}, {3, 1, {{{1, {{{{5, {3, 1}}, {{{{5, {3, 1}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 3, 1}}}}, {3, 4, 6, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 1}}}}, 1}}, {2, 1}}, {1, {2, {{{5, {3, 10}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 3, 1}}}}, 1}}}}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 4, 1}}}}}, {{{5, {3, 1}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 3, 1}}}}}}, {{{1, {{{{{{{5, {3, 1}}, {2, {{{{1, {{{{5, {3, 1}}, {{{{5, {3, 1}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 3, 1}}}}, {3, 4, 6, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 1}}}}, 1}}, {2, 1}}, {1, {2, {{{5, {3, 10}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 3, 1}}}}, 1}}},
               1, 1}}}, {{{1, {2, 1}}, {2, 1}}, {1, 9, {2, 3, 1}}}}, {{{{5, {3, 1}}, {2, {2, 1, 1, 8}, {{{5, {2, 1}}, {2, {3, 1, 1}}}, {{{1, {1, 1}}, {2, 1}}, {1, {2, 3, 1}}}}}}, {{{1, {{{{5, {3, 1}}, {{{{5, {3, 1}}, {{{5, {3, 1}}, {{{{5, {3, 1}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 3, 1}}}}, {3, 4, 6, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 1}}}, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 3, 1}}}}, {3, 4, 6, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 1, 1}}}}, 1}}, {2, 1}}, {1, {2, {{{5, {3, 10}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, {2, 3, 1}}}}, 1}}}},
            1}}, {2, {3, 1, 1}}}, {{{1, {3, 1}}, {2, 1}}, {1, 4, 1, 2, {2, 3, 1}}}}, 1}}, {2, 1}}, {1, {2, 3, 1}}}};
ResourceFunction["TreemapPlot"][tree,
 "AspectRatio" -> N@GoldenRatio, "ImageWidth" -> 300,
 "ColorFunction" -> (ColorData["Rainbow"][#2/Max@tree] &), "Background" -> FaceForm[Opacity[1]], "FrameStyle" -> EdgeForm[{Thin, GrayLevel[.6]}]]
Out[8]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 07 April 2020

License Information