matplotlib: difference between pcolor, pcolormesh and imshow

If you have a matrix and want to plot its content as an image, matplotlib provides some functions such as imshow and pcolor.
c

Differences between imshow and pcolor

Let us use a simple 3 by 3 matrix and call imshow and pcolor.

Note the values of the data :

Here are the figures; color code is: dark red=1, yellow=0.5, dark blue=0

imshow

pcolor

So, the main differences are:

  • imshow follows a convention used in image processing: the origin is in the top left corner. So the value 0.72 ( first row and first column in the matrix) appears in the top left corner. pcolor has a different convention; that is why we used the function flipud in the code above so that the two figures look similar.
  • pixel locations are different as you can see on the x/y axis: placed at position 0,1,2 in imshow and between integer location in pcolor
  • parameters used in the 2 functions are different and we let the reader look at the documentation for more details
  • (update oct 2018). imshow function is also 4-5 times faster than pcolor (thanks to a comment from norok2) from matrices with dimension above 20-30. One can use the script here below to confirm this statement.

Differences between pcolor and pcolormesh

The 2 functions are almost identical. There are two main differences. The returned object differs: pcolor returns a class
~matplotlib.collections.PolyCollection but pcolormesh returns a class ~matplotlib.collections.QuadMesh.

However, the main important difference is that pcolormesh is much faster by several order, as you can see in the figure below, which can be re-generated with the following code:

pcolor_performance

As suggested by a few comments, the next question is why shall we use pcolor instead of pcolormesh ? From pcolormesh, an additional difference is

Yet, I have not encoutered a case where pcolor should be used instead of pcolormesh.

This entry was posted in Python and tagged . Bookmark the permalink.

9 Responses to matplotlib: difference between pcolor, pcolormesh and imshow

  1. Another reason to use pcolormesh is that you can set the NA (if any) to have a different
    color from the colormap.

    For example, once you have a colormap (let us call it cmap), type:

    cmap.set_bad(‘grey’)

  2. Tommy Carstensen says:

    Why does pcolor even exist, if pcolormesh is superior?

    • Absay says:

      Why do bicycles even exist, if planes are superior?

      • vincenzooo says:

        I don’t know why bicycles exist, but I doubt it would help me to understand pcolor vs pcolormesh.
        The question can be restated as: “is there any case in which I want to use pcolor instead of pcolormesh?”

  3. Pingback: Python:matplotlib.pcolor very slow. alternatives? – IT Sprite

  4. Daniel Watkins says:

    I’m working on plotting in Basemap, and if the latitude and longitude are masked arrays, pcolormesh will sometimes distort shapes while pcolor plots them correctly. I haven’t figured out what is causing the difference yet, but it’s at least one case where pcolor is working better than pcolormesh.

  5. norok2 says:

    It should be mentioned that imshow is faster than pcolormesh

Leave a Reply

Your email address will not be published.