Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 42 ยท Basic Charts

Sunburst, Treemap and Icicle Charts

Show hierarchical data with Plotly sunburst, treemap and icicle charts: use path or ids, labels and parents, branchvalues, color by value, maxdepth and click-to-drill-down.

Some data is a tree: continents contain countries, a company has departments with teams, a disk has folders and files. Pie charts cannot show more than one level, but three Plotly chart types can: sunburst (rings), treemap (nested rectangles) and icicle (stacked rectangles, like a flame graph turned upside down). In all three, the size of an item represents its value, and children are drawn inside or beyond their parent.

Click an item to zoom into it, and click the center (or the path bar) to go back up. This built-in drill-down is the main reason to choose these charts over static alternatives.

Express: the path argument

With a tidy DataFrame, you do not describe the tree explicitly. Name the columns that make the hierarchy, from root to leaf, in path, and the column that gives each leaf its size in values:

Python px.sunburst, px.treemap and px.icicle from a table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Express built the tree from the columns. Every distinct combination becomes a node with an id (the full path), a label, a parent id and a value. The top level nodes have an empty parent. The sums of the leaves are rolled up the tree: the value of "Sat" is the total of its meals. px.Constant("all bills") adds a single root so the chart has one starting point.

The same chart from labels and parents

Without a DataFrame, or if your data is already a tree table, give plotly.js three lists: labels, parents and values. Each item names its parent, and the root item has "" as parent:

Sunburst and treemap from labels and parents

The important setting is branchvalues:

branchvalues Meaning
"remainder" (default for graph objects) A parent's own value is added to its children's total. Parents with value 0 take their size from the children.
"total" A parent's value is the total including its children, so children must add up to at most the parent. Express uses this.

With total, a mismatch (children bigger than the parent) makes plotly.js refuse to draw the sector, a common source of empty charts.

Choosing between them

Chart Strength
Sunburst Shows levels as rings; good for few levels and a visual overview of the structure.
Treemap Uses the whole area efficiently; best for many small items and comparing leaf sizes.
Icicle Easiest to read labels, since rectangles line up in rows; good for long names.

Readers compare rectangle areas better than ring segments, so treemaps are usually more accurate for comparing sizes. Sunbursts are more attractive and clearer about structure.

Color and size options

  • color="column" colors by a numeric or categorical column. For numeric columns, the color of a parent is the weighted average of its children, which is useful for showing a rate (profit margin, score) on top of a size (revenue).
  • color_continuous_scale, color_continuous_midpoint and range_color control the gradient.
  • maxdepth=2 draws only a few levels at once and reveals deeper ones when the user clicks.
  • hover_data and hovertemplate customize the tooltip. For sunburst and treemap, %{percentParent}, %{percentRoot} and %{percentEntry} are available.
  • textinfo="label+percent parent" chooses which text is printed on items.
Python Color by an average, limit the depth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The root value (in billions) is the world's population in 2007. The rectangle size is the population, while the color shows the life expectancy, so the chart shows two variables over a hierarchy.

Preparing hierarchical data

  • A table with one row per leaf, where higher levels are columns (as above), is the easiest input.
  • Use groupby to aggregate before plotting when you only need some levels.
  • Missing values in path columns cause errors or empty nodes. Fill them with a label such as "unknown" first.
  • A tree where the same name appears under different parents is fine in Express, because ids hold the full path ("Europe/France"). With the labels and parents input you should provide unique ids too.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In Express, which argument defines the hierarchy of a sunburst?

    path lists the columns from the root level to the leaf level.
  2. 2. What is the effect of branchvalues="total"?

    With total, the parent's value is the total of the branch. Children larger than their parent cannot be drawn.
  3. 3. What does a root node look like in the parents list?

    Top-level items have an empty string as parent.
  4. 4. Which chart type is usually best for comparing the sizes of many small leaf items?

    Rectangle areas are easier to compare than ring segments, and treemaps waste little space.
  5. 5. How do you show only the first two levels until the user clicks deeper?

    maxdepth limits the number of levels drawn at once.

Summary

  • Sunburst, treemap and icicle charts show tree data, with size by value and click-to-zoom.
  • In Express use path and values; in graph objects use labels, parents, values (and ids).
  • branchvalues decides whether a parent's value includes its children.
  • Color by another column to show a second variable, and use maxdepth to keep the first view simple.

Next: Funnel, Waterfall and Candlestick Charts