• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How to Make Heatmap with Matplotlib in Python

datavizpyr · July 19, 2022 ·

Last updated on August 29, 2025

How to Make a Heatmap in Python with Matplotlib (Step-by-Step Guide)

Heatmaps are a powerful way to visualize matrices and time-based data. They let you quickly spot trends, patterns, and seasonality in numbers. While libraries like Seaborn provide a high-level heatmap() function, Matplotlib’s imshow() gives you low-level control and flexibility to customize every part of your plot.

In this tutorial, we’ll create a heatmap using imshow() with real-world Flights data from Seaborn. We’ll start simple and progressively add labels, colorbars, and custom colormaps to make it publication-quality.

Step 1: Import Libraries

We’ll use Matplotlib, NumPy, Pandas, and Seaborn for dataset loading.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

Step 2: Load and Prepare the Flights Dataset

We will use flights dataset available in Seaborn’s built-in datasets. The flights dataset records monthly passengers between 1949 and 1960. We pivot the data so that months become rows and years become columns (matrix form).

flights = sns.load_dataset("flights")
# pivoting to make the data wide
flights = flights.pivot("month", "year", "passengers")
flights.head()


year	1949	1950	1951	1952	1953	1954	1955	1956	1957	1958	1959	1960
month												
Jan	112	115	145	171	196	204	242	284	315	340	360	417
Feb	118	126	150	180	196	188	233	277	301	318	342	391
Mar	132	141	178	193	236	235	267	317	356	362	406	419
Apr	129	135	163	181	235	227	269	313	348	348	396	461
May	121	125	172	183	229	234	270	318	355	363	420	472

Step 3: Simple Heatmap with imshow()

Once in matrix form, we can display the data directly with imshow(). This creates a basic heatmap.

fig, ax = plt.subplots(figsize=(10,10))
im = ax.imshow(flights)

Let us add simple annotation first to make the heatmap slightly better. Here we have added a title to the heatmap using set_title() function.

fig, ax = plt.subplots(figsize=(8,6))
im = ax.imshow(flights)
ax.set_title("Matplotlib Heatmap with imshow", size=16)
fig.tight_layout()
plt.savefig("heatmap_in_matplotlib_using_imshow.png",
                    format='png',dpi=150)
How to make heatmap with Matplotlib
How to make heatmap with Matplotlib

Note: At this stage, the axes don’t yet show month/year labels.

Note that x and y axis labels are missing.

Step 4: Add Axis Labels

To add the axis labels let us get the months and years values from the row and column names of flights dataset. We add month names on the y-axis and years on the x-axis using set_xticks() and set_yticks().

months = flights.index.values
months
years = flights.columns.values

Now we can add the years on x -axis and months on y-axis tick labels. In Matplotlib, we can add the tick labels using set_xticks() and set_yticks() functions.

fig, ax = plt.subplots(figsize=(10,10))
im = ax.imshow(flights)
# Add axis tick labels
ax.set_xticks(np.arange(len(years)), 
              labels=years)
ax.set_yticks(np.arange(len(months)), 
              labels=months)
ax.set_title("Adding axis labels to Matplotlib Heatmap", 
             size=20)
fig.tight_layout()
plt.savefig("axis_labels_in_heatmap_in_matplotlib.png",
                    format='png',dpi=150)
Adding axis labels to heatmap with Matplotlib
Adding axis labels to heatmap with Matplotlib

Step 5: Add a Colorbar

A colorbar makes the heatmap interpretable by linking colors to numeric values. We can add color bar legend to help understand the range of numerical values and their association with the colors to the heatmap using figure.colorbar() function. Sometimes the color bar added can be slightly bigger than the heatmap. Here we have use shrink argument to reduce the size of the color bar.

fig, ax = plt.subplots( figsize=(10,10))
im = ax.imshow(flights)
cbar = ax.figure.colorbar(im, 
                          ax = ax,
                          shrink=0.5 )
# add tick labels
ax.set_xticks(np.arange(len(years)), 
              labels=years, 
              size=12)
ax.set_yticks(np.arange(len(months)),
              labels=months,size=12)
# Rotate the tick labels to be more legible
plt.setp(ax.get_xticklabels(),
         rotation = 45,
         ha = "right",
         rotation_mode = "anchor")
ax.set_title("Flights Data Seaborn", size=20)
fig.tight_layout()
plt.savefig("how_to_make_a_heatmap_with_matplotlib_Python.png",
                    format='png',dpi=150)
How to make a heatmap with imshow() in Matplotlib
How to make a heatmap with imshow() in Matplotlib

Step 6: Customize the Color Palette

By default, Matplotlib’s imshow() use viridis color palette to make the heatmap. You can change the colormap using the cmap argument. Here we use the YlGn palette for a green gradient.

fig, ax = plt.subplots( figsize=(10,10))
im = ax.imshow(flights, cmap="YlGn")
cbar = ax.figure.colorbar(im,
                          ax=ax,
                          shrink=0.5 )
# add tick labels
ax.set_xticks(np.arange(len(years)), 
              labels=years, 
              size=12)
ax.set_yticks(np.arange(len(months)), 
              labels=months,
              size=12)
# Rotate the tick labels to be more legible
plt.setp(ax.get_xticklabels(), 
         rotation=45, 
         ha="right",
         rotation_mode="anchor")
ax.set_title("Flights Data Seaborn", size=20)
fig.tight_layout()
plt.savefig("change_heatmap_color_palette_matplotlib_Python.png",
                    format='png',dpi=150)
How to Change Heatmap Color Palette in Matplotlib
Change Heatmap Color Palette in Matplotlib using cmap argument

FAQs

  • How do I change the color palette of a heatmap in Matplotlib?
    Use the cmap argument in imshow(), e.g., cmap="coolwarm" or cmap="magma". You can also create custom colormaps with matplotlib.colors.
  • How can I add numeric values inside a heatmap?
    Loop over the matrix with ax.text() to annotate each cell. Example:
    ax.text(j, i, flights.iloc[i, j], ha="center", va="center").
  • How do I control the color scale range in imshow?
    Use vmin and vmax arguments in imshow(). For example, imshow(data, vmin=100, vmax=500) ensures consistent color scaling across multiple plots.
  • How do I reverse the color scale in a heatmap?
    Append _r to the colormap name (e.g., cmap="viridis_r") to flip it. This is useful if you want high values in lighter colors instead of darker.
  • How can I improve readability of tick labels in large heatmaps?
    Rotate labels with plt.setp(ax.get_xticklabels(), rotation=45, ha="right"). For long labels, reduce font size using fontsize in set_xticks().
  • How do I export heatmaps as high-resolution images?
    Use plt.savefig("heatmap.png", dpi=300, bbox_inches="tight") to get a publication-quality PNG or PDF.
  • How do I normalize values across multiple heatmaps for comparison?
    Specify a shared vmin and vmax for all heatmaps, so the same color scale is applied to each figure.
  • How do I add a custom colorbar with specific ticks?
    Create a colorbar and then set ticks with cbar.set_ticks([100, 200, 300]) and cbar.set_ticklabels(["Low", "Medium", "High"]).

Do you want to learn how to build heatmaps easily with Seaborn? Check out our guide on Heatmaps with Seaborn in Python.

Related posts:

Combine Two plots into one in SeabornHow to Combine Two Seaborn plots with Shared y-axis Connect paired data points in a scatter plot with arrowheads and labelsConnect Paired Data Points in a Scatter Plot in Python (Step-by-Step Guide) Change matplotlib style scatterplot to fivethirtyeightHow to View All Matplotlib Plot Styles and Change Grouped barplot with Matoplotlib in PythonHow to make Grouped barplots with Matplotlib in Python

Filed Under: Matplotlib, Python Tagged With: heatmaps with Matplotlib

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version