Heatmaps

Matplotlib Basics

1 min read

Published Oct 5 2025


15
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonVisualisation

A heatmap is a graphical representation of a matrix or 2D dataset where colours represent the magnitude of values.

  • Rows and columns correspond to indices of the data matrix.
  • Colour intensity encodes numerical values.
  • Useful for visualising correlation matrices, grids, or spatial data.

Syntax:

plt.imshow(data, cmap=None, interpolation='nearest', origin='upper')

Parameters:

  • data = 2D array (NumPy array, list of lists, or Pandas DataFrame)
  • cmap = Colourmap (e.g., 'viridis', 'coolwarm')
  • interpolation = Pixel interpolation ('nearest' is common)
  • origin = 'upper' (default) or 'lower' for y-axis origin

To add a colour scale, use plt.colorbar().




Basic heatmap example

import matplotlib.pyplot as plt
import numpy as np

# 5x5 matrix of random values
data = np.random.rand(5, 5)

plt.imshow(data, cmap='viridis')
plt.colorbar()
plt.title("Basic Heatmap")
plt.show()

Each cell’s colour corresponds to its value.


matplotlib heatmap chart basic example




Adjusting colourmaps

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(5, 5)

plt.imshow(data, cmap='coolwarm')
plt.colorbar()
plt.title("Heatmap with 'coolwarm' Colourmap")
plt.show()

Popular colourmaps: 'viridis', 'plasma', 'inferno', 'magma', 'cividis', 'coolwarm', 'RdYlBu'.


matplotlib heatmap chart coolwarm example




Control colour scale (vmin/vmax)

You can fix the colour scale to compare multiple heatmaps:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(5, 5)

plt.imshow(data, cmap='viridis', vmin=0, vmax=1)
plt.colorbar()
plt.title("Fixed Colour Scale Heatmap")
plt.show()

matplotlib heatmap chart fixedscale example




Display values on each cell

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(5, 5)

for i in range(data.shape[0]):
    for j in range(data.shape[1]):
        plt.text(j, i, f"{data[i, j]:.2f}",
                 ha='center', va='center', color='white')

plt.imshow(data, cmap='viridis')
plt.colorbar()
plt.title("Heatmap with Values")
plt.show()

Adjust color='white' or 'black' depending on background contrast.


matplotlib heatmap chart values example




Horizontal and vertical axis labels

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(5, 5)

plt.imshow(data, cmap='viridis')
plt.colorbar()
plt.xticks(range(5), ['A','B','C','D','E'])
plt.yticks(range(5), ['W','X','Y','Z','V'])
plt.title("Heatmap with Axis Labels")
plt.show()

matplotlib heatmap chart axislabels example




Aspect ratio and grid

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(5, 5)

plt.imshow(data, cmap='plasma', aspect='auto')
plt.colorbar()
plt.title("Heatmap with Custom Aspect")
plt.show()

  • aspect='equal' → square cells
  • aspect='auto' → fills plot area

Add gridlines using plt.grid() if desired (less common).


matplotlib heatmap chart custom aspect example




Using pcolormesh for heatmaps

pcolormesh allows non-uniform grids and finer control:

import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(np.arange(6), np.arange(6))
Z = np.random.rand(6, 6)

plt.pcolormesh(X, Y, Z, cmap='coolwarm', shading='auto')
plt.colorbar()
plt.title("Heatmap with pcolormesh")
plt.show()

matplotlib heatmap chart pcolormesh example




Using pandas DataFrame directly

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

df = pd.DataFrame(np.random.rand(4, 4), columns=list('ABCD'), index=list('WXYZ'))

plt.imshow(df, cmap='viridis')
plt.colorbar()
plt.xticks(range(df.shape[1]), df.columns)
plt.yticks(range(df.shape[0]), df.index)
plt.title("Heatmap from DataFrame")
plt.show()

matplotlib heatmap chart pandas dataframe example

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact