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.

plotly-logo

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 plotly.express as px
import plotly.graph_objects as go

plotly-treemap-load-data.PNG

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

plotly-univariate-treemap

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

plotly-multivariate-treemap.png

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.

Treemaps in Plotly

Post navigation


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