I often find myself needing to create heatmap-style visualizations in Python with matplotlib. Matplotlib provides several functions which apparently do the same thing.
Fundamentally, imshow
assumes that all data elements in your array are to be rendered at the same size, whereas pcolormesh
/pcolor
associates elements of the data array with rectangular elements whose size may vary over the rectangular grid.
If your mesh elements are uniform, then imshow
with interpolation set to "nearest" will look very similar to the default pcolormesh
display (without the optional X
and Y
args). The obvious differences are that the imshow
y-axis will be inverted (w.r.t. pcolormesh
) and the aspect ratio is maintained, although those characteristics can be altered to look like the pcolormesh
output as well.
From a practical point of view, pcolormesh
is more convenient if you want to visualize the data array as cells, particularly when the rectangular mesh is non-uniform or when you want to plot the boundaries/edges of the cells. Otherwise, imshow
is more convenient if you have a fixed cell size, want to maintain aspect ratio, want control over pixel interpolation, or want to specify RGB values directly.