Tree Maps for Hierarchical Data
Tree maps display hierarchical data as nested rectangles. Rectangle size represents quantity, making it easy to spot largest contributors and compare proportions at a glance.
Creating a Tree Map in Python
import plotly.express as px
import pandas as pd
# Sales by category and subcategory
data = pd.DataFrame({
'category': ['Electronics', 'Electronics', 'Clothing', 'Clothing', 'Food', 'Food'],
'subcategory': ['Phones', 'Laptops', 'Shirts', 'Pants', 'Snacks', 'Drinks'],
'sales': [45000, 38000, 22000, 18000, 15000, 12000]
})
fig = px.treemap(data,
path=['category', 'subcategory'],
values='sales',
title='Sales by Category')
fig.show()
When to Use Tree Maps
- Showing part-to-whole relationships
- Comparing proportions hierarchically
- Space-efficient display of many categories
- Portfolio allocation
- File system sizes
Tree Map Best Practices
- Use color for categories or values
- Show labels on larger rectangles only
- Limit hierarchy to 2-3 levels
- Keep total categories under 20
Pro Tip: Tree maps work best for showing proportions of a whole. Use color to encode a second variable (like growth rate) for added insight. Avoid when precise comparison is needed—use bar charts instead!
← Back to Visualization Tips