Heatmaps
Matplotlib Basics
1 min read
This section is 1 min read, full guide is 24 min read
Published Oct 5 2025
15
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
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')
Copy to Clipboard
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()
Copy to Clipboard
Toggle show comments
Each cell’s colour corresponds to its value.

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()
Copy to Clipboard
Popular colourmaps: 'viridis', 'plasma', 'inferno', 'magma', 'cividis', 'coolwarm', 'RdYlBu'.

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()
Copy to Clipboard

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()
Copy to Clipboard
Adjust color='white' or 'black' depending on background contrast.

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()
Copy to Clipboard

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()
Copy to Clipboard
aspect='equal'→ square cellsaspect='auto'→ fills plot area
Add gridlines using plt.grid() if desired (less common).

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()
Copy to Clipboard

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()
Copy to Clipboard















