Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use imshow over pcolormesh?

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?

like image 621
mmdanziger Avatar asked Jan 16 '14 15:01

mmdanziger


People also ask

What does Pyplot Imshow do?

imshow() Function: The imshow() function in pyplot module of matplotlib library is used to display data as an image; i.e. on a 2D regular raster.

What is Pcolormesh?

pcolormesh() Function: The pcolormesh() function in pyplot module of matplotlib library is used to create a pseudocolor plot with a non-regular rectangular grid. Syntax: matplotlib.pyplot.pcolormesh(\*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, shading='flat', antialiased=False, data=None, \*\*kwargs)

What is Imshow return?

As you have already found out, the return type of plt. imshow() is a matplotlib. image.


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.

like image 61
bogatron Avatar answered Sep 29 '22 19:09

bogatron