This post extends the post on Treemap in matplotlib. In data visualization Treemap is a pictorial representation of hierarchical data using nested rectangle figures. Treemap uses colours to distinguish between different data dimensions being visualized. To divide each region into sub-regions Treemap uses a tiling algorithm. Plotly comes with out-of-the box library for easily creating interactive treemaps. In this post we will look at treemaps, when to use them and how to create them in plotly.


When to Use Treemaps

  1. Treemaps are used to visualize hierarchical data.
  2. They are suitable in showing the part-to-whole relationship for large groups.
  3. Treemaps are ideal in cases where there is no enough space for multiple visualizations.

How to Use Treemaps

  1. Works well when there are fewer categories.
  2. The values needs to be positive.
  3. Select colours that blend well with each group and other dashboard components.

Treemaps in Ploty

Load Required Libraries


import numpy as np
import pandas as pd
import as px
import plotly.graph_objects as go


Univariate Treemap with Plotly 


fig = px.treemap(salary_df, path=['Staff'], values='Salary')

fig.update_layout(title={'text': 'Staff Salary Treemap','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
                  font=dict(size=20, family='Times New Romans', color='brown'),


Multi-variate Treemap with Plotly


fig = px.treemap(salary_df, path=[‘Department’,’Staff’], values=’Salary’,
color=’Staff’, color_continuous_scale=’RdBu’,)

fig.update_layout(title={‘text’: ‘Department Staff Salary Treemap’,’y’:0.95,’x’:0.5, ‘xanchor’: ‘center’,’yanchor’: ‘top’},
font=dict(size=20, family=’Times New Romans’, color=’brown’),

fig.update_xaxes(showline=True, linewidth=1, linecolor=’white’, gridwidth=3, gridcolor=’white’, mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor=’white’, gridwidth=3, gridcolor=’white’, mirror=True)


For complete code check the jupyter notebook here.


Treemaps have many use-cases in data analytics. They are primarily used to visualize hierarchical data. Treemaps are suitable to present data in cases where there is no space for many visualizations but need to communicate insights to users. In this post we have looked at Treemaps, when to use them and how to create them in plotly. In the next post we will look at geospatial analysis in plotly. To learn about Heatmap in plotly check our previous post here.

Treemaps in Plotly

Post navigation

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x