AI Hints
Home›Data Science Tutorial›Better Charts and Your Career
Data Science · Lesson 79 of 101

Heatmaps

Two dimensions at a glance.

⏱ 2 min read▶ Video at 2:13:59🎓 Free
Watch this lesson in the Data Science courseWatch on YouTube ↗Full Data Science course

Which product sells best on which day? That's eight products and seven days, so fifty six numbers. As a table, nobody would read it. As a heatmap, the pattern jumps out instantly.

Heatmaps
From the video: Heatmaps
  • Eight products, seven days
  • Fifty six numbers
  • The pattern jumps out

A product by weekday heatmap

We build a table of average units, by product and weekday, and put the days in their proper order. Seaborn then colours each cell. Darker means more. We keep the numbers in the cells too. Run it. Straight away, you can see that Saturday and Sunday are the darkest columns, and that latte's the darkest row. A manager can use this to plan staff and stock. Use heatmaps for tables, for correlation matrices, and for retention cohorts.

heatmap.pypython
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sales = pd.read_csv("sales.csv", parse_dates=["date"])
sales["weekday"] = sales["date"].dt.day_name().str[:3]
t = sales.pivot_table(index="product", columns="weekday", values="units", aggfunc="mean")
t = t[["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]]

plt.figure(figsize=(8, 4.2))
sns.heatmap(t, annot=True, fmt=".0f", cmap="YlOrBr")
plt.tight_layout(); plt.savefig("heat.png", dpi=110)
print("Busiest:", t.mean().idxmax())
Output
Busiest: Sat

Summary

Colour turns a table into a picture. Your charts are now at a professional level. Next, the final push before the capstone. Getting the job.

Key takeaways
  • Heatmaps show two dimensions
  • Keep numbers in the cells
  • Great for cohorts and correlations
Next up: Harder SQL Questions — the interview round