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
- Treemaps are used to visualize hierarchical data.
- They are suitable in showing the part-to-whole relationship for large groups.
- Treemaps are ideal in cases where there is no enough space for multiple visualizations.
How to Use Treemaps
- Works well when there are fewer categories.
- The values needs to be positive.
- 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 plotly.express as px
import plotly.graph_objects as go
Univariate Treemap with Plotly
fig = px.treemap(salary_df, path=['Staff'], values='Salary')
fig.update_traces(root_color="lightgrey")
fig.update_layout(title={'text': 'Staff Salary Treemap','y':0.95,'x':0.5, 'xanchor': 'center','yanchor': 'top'},
autosize=True,margin=dict(t=70,b=0,l=0,r=0),
font=dict(size=20, family='Times New Romans', color='brown'),
)
fig.show()
Multi-variate Treemap with Plotly
fig = px.treemap(salary_df, path=[‘Department’,’Staff’], values=’Salary’,
color=’Staff’, color_continuous_scale=’RdBu’,)
fig.update_traces(root_color=”lightgrey”)
fig.update_layout(title={‘text’: ‘Department Staff Salary Treemap’,’y’:0.95,’x’:0.5, ‘xanchor’: ‘center’,’yanchor’: ‘top’},
autosize=True,margin=dict(t=70,b=0,l=0,r=0),
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)
fig.show()
For complete code check the jupyter notebook here.
Conclusion
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.