Useful Data Tips

Tree Maps for Hierarchical Data

⏱️ 23 sec read 📊 Data Visualization

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

Tree Map Best Practices

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