liveBook · Manning

Heat map

As explained in Starting Data Analytics with Generative AI and Python

A heatmap is a graphical display that represents numerical values through colors arranged in a grid.

Which product categories sell strongly in which regions? A heat map turns a table of numbers into a grid of colored cells. Put one category on each axis, then use color to make high and low values easier to spot without reading every number. section 3.3.1 section 4.2

For sales analysis, first calculate a measure such as sales volume or revenue by state and product category. Pivot the results so states and categories form the grid, then plot that table with Seaborn. Keep volume and revenue in separate heat maps: they answer different questions. section 3.3.1

import seaborn as sns
import matplotlib.pyplot as plt

# Create pivot tables for sales_volume and revenue
sales_volume_pivot = sales_by_region.pivot_table(
values='sales_volume', index='customer_state', 
columns='product_category_name_english')
revenue_pivot = sales_by_region.pivot_table(
values='revenue', index='customer_state', 
columns='product_category_name_english')

# Plot heatmaps
plt.figure(figsize=(12, 8))
sns.heatmap(sales_volume_pivot, annot=True, cmap='viridis', fmt='.0f')
plt.title('Sales Volume by Product Category and Region')
plt.show()

plt.figure(figsize=(12, 8))
sns.heatmap(revenue_pivot, annot=True, cmap='viridis', fmt='.0f')
plt.title('Revenue by Product Category and Region')
plt.show()

The Python workflow creates separate state-by-category pivot tables for sales volume and revenue, then plots each with annotated Seaborn heat maps. section 3.3.1

Read the broad pattern first, then investigate particular cells:

  • Look for an exceptionally high cell: it may identify a product category performing especially well in one region. section 4.2
  • Compare differences across states and categories, but treat possible business explanations as questions to investigate rather than facts established by the colors. section 4.2 section 4.5
  • Check surprising results against the underlying table. If AI-generated plotting code errors or the output looks odd, inspect it and report the specific problem rather than accepting a proposed fix blindly. section 3.3.1 section 4.2

A data-quality heat map

Heat maps can reveal more than sales. Mark each DataFrame value as missing or present, calculate correlations between those missing-value indicators, and plot the correlation matrix. Strong positive values point to columns whose missing entries tend to occur together. In the sample code, the DataFrame is created as `vdf` but later referenced as `df`; use one variable name before running it. section 2.5.2

A delivery-time heat map offers another starting point: much higher average times in some states are worth investigating. Describe the observed pattern precisely when asking an AI assistant for possible causes or actions, and verify its suggestions before making a business decision. section 4.5

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through missingness correlations and sales pivot tables, then interprets visible patterns without developing heatmap mathematics.
Examples
Uses state-by-product-category sales grids, a missing-value correlation grid, and a delivery-time comparison.
What it assumes
Assumes basic familiarity with Python DataFrames while demonstrating missingness indicators and pivot-table preparation through examples.
What you can do after
Apply it via pandas and Seaborn to plot a category-by-state grid, then inspect unusual cells and validate interpretations.
How it connects
Connects heat maps to pivot tables, correlation matrices, missing-data analysis, sales analysis, and delivery-time analysis.

Also explained in

Compare how 7 books explain heat map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.