I often find myself needing to create heatmap-style visualizations in Python with matplotlib. Matplotlib provides several functions which apparently do the same thing. pcolormesh
is recommended instead of pcolor
but what is the difference (from a practical point of view as a data plotter) between imshow
and pcolormesh
? What are the pros/cons of using one over the other? In what scenarios would one or the other be a clear winner?

- 4,466
- 2
- 31
- 47
1 Answers
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.

- 18,639
- 6
- 53
- 47
-
20+1 Just to add a bit: The aspect and flipping of the y-axis are easily controlled with keyword arguments to imshow (`aspect='auto'` and `origin='lower'` respectively). In addition to what you mentioned, other major differences are: speed (`imshow` is much more efficient), the ability/inability to display the cell edges (`pcolor`/`pcolormesh` can, `imshow` can't), and the centering of the cells (`pcolormesh` aligns the cells on their edges, while `imshow` aligns them on their centers. This is controlled by the `extent` kwarg or using x & y arguments to `pcolormesh`.) – Joe Kington Jan 16 '14 at 18:23
-
1Also, `pcolormesh` isn't limited to just rectangular cells. They can actually be any quadrilateral if you pass in the `x`, `y`, and `z` parameters. – Joe Kington Jan 16 '14 at 18:24
-
Thanks for the additional info. I suspected `imshow` is faster but didn't bother to test that. Just to clarify, when you say `z` parameter, I assume you mean the `C` parameter (i.e., `pcolormesh` can't generate a 3D mesh). – bogatron Jan 16 '14 at 18:40
-
Yeah, I mean `C` rather than `z`, though that wasn't clear at all from my comment. – Joe Kington Jan 16 '14 at 18:56
-
Do you know if there is a blog post out there with visual examples comparing `imshow` and `pcolormesh`? Thanks! – tommy.carstensen Jan 18 '16 at 16:04
-
3`pcolormesh` also works nicely when one of your axis is a `datetime`. `imshow` doesn't like this and you have to convert to a pure numeric time representation. – Tom Johnson Sep 05 '16 at 09:40
-
1@tommy.carstensen Check out this link: http://thomas-cokelaer.info/blog/2014/05/matplotlib-difference-between-pcolor-pcolormesh-and-imshow/ – blalterman Jul 11 '17 at 15:45
-
@blalterman Thanks! The page reads: "the main important difference is that pcolormesh is much faster". – tommy.carstensen Jul 11 '17 at 21:07
-
3just to make it clear: `pcolor` is slower than `pcolormesh`, which is itself slower than `imshow` (at least on Matplotlib 2.2.0). – norok2 Mar 15 '18 at 15:19