Matplotlib 2D Image Plotting with pcolormesh

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this tutorial, we will learn how to use the pcolormesh function in the Matplotlib library to generate 2D image-style plots. We will cover the basic usage of pcolormesh, non-rectilinear pcolormesh, centered coordinates, and making levels using norms.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/SpecializedPlotsGroup(["`Specialized Plots`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/SpecializedPlotsGroup -.-> matplotlib/contour_plots("`Contour Plots`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/with_statement -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} matplotlib/importing_matplotlib -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} matplotlib/figures_axes -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} matplotlib/contour_plots -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/booleans -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/for_loops -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/lists -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/tuples -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/importing_modules -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/using_packages -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/standard_libraries -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/math_random -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/numerical_computing -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/data_visualization -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} python/build_in_functions -.-> lab-48860{{"`Matplotlib 2D Image Plotting with pcolormesh`"}} end

Basic pcolormesh

We usually specify a pcolormesh by defining the edge of quadrilaterals and the value of the quadrilateral. Note that here x and y each have one extra element than Z in the respective dimension.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
Z = np.random.rand(6, 10)
x = np.arange(-0.5, 10, 1)  ## len = 11
y = np.arange(4.5, 11, 1)  ## len = 7

fig, ax = plt.subplots()
ax.pcolormesh(x, y, Z)

Non-rectilinear pcolormesh

Note that we can also specify matrices for X and Y and have non-rectilinear quadrilaterals.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
Z = np.random.rand(6, 10)
x = np.arange(-0.5, 10, 1)  ## len = 11
y = np.arange(4.5, 11, 1)  ## len = 7
X, Y = np.meshgrid(x, y)
X = X + 0.2 * Y  ## tilt the coordinates.
Y = Y + 0.3 * X

fig, ax = plt.subplots()
ax.pcolormesh(X, Y, Z)

Centered coordinates

Often a user wants to pass X and Y with the same sizes as Z to .axes.Axes.pcolormesh. This is also allowed if shading='auto' is passed (default set by :rc:pcolor.shading). Pre Matplotlib 3.3, shading='flat' would drop the last column and row of Z, but now gives an error. If this is really what you want, then simply drop the last row and column of Z manually:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)
Z = np.random.rand(6, 10)
x = np.arange(10)  ## len = 10
y = np.arange(6)  ## len = 6
X, Y = np.meshgrid(x, y)

fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)
axs[0].pcolormesh(X, Y, Z, vmin=np.min(Z), vmax=np.max(Z), shading='auto')
axs[0].set_title("shading='auto' = 'nearest'")
axs[1].pcolormesh(X, Y, Z[:-1, :-1], vmin=np.min(Z), vmax=np.max(Z),
                  shading='flat')
axs[1].set_title("shading='flat'")

Making levels using norms

Shows how to combine Normalization and Colormap instances to draw "levels" in .axes.Axes.pcolor, .axes.Axes.pcolormesh and .axes.Axes.imshow type plots in a similar way to the levels keyword argument to contour/contourf.

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator

## make these smaller to increase the resolution
dx, dy = 0.05, 0.05

## generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(1, 5 + dy, dy),
                slice(1, 5 + dx, dx)]

z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x)

## x and y are bounds, so z should be the value *inside* those bounds.
## Therefore, remove the last value from the z array.
z = z[:-1, :-1]
levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())


## pick the desired colormap, sensible levels, and define a normalization
## instance which takes data values and translates those into levels.
cmap = plt.colormaps['PiYG']
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

fig, (ax0, ax1) = plt.subplots(nrows=2)

im = ax0.pcolormesh(x, y, z, cmap=cmap, norm=norm)
fig.colorbar(im, ax=ax0)
ax0.set_title('pcolormesh with levels')


## contours are *point* based plots, so convert our bound into point
## centers
cf = ax1.contourf(x[:-1, :-1] + dx/2.,
                  y[:-1, :-1] + dy/2., z, levels=levels,
                  cmap=cmap)
fig.colorbar(cf, ax=ax1)
ax1.set_title('contourf with levels')

## adjust spacing between subplots so `ax1` title and `ax0` tick labels
## don't overlap
fig.tight_layout()

plt.show()

Summary

In this tutorial, we learned how to use the pcolormesh function in the Matplotlib library. We covered the basic usage of pcolormesh, non-rectilinear pcolormesh, centered coordinates, and making levels using norms. These techniques can be used to generate different types of 2D image-style plots in Matplotlib.

Other Matplotlib Tutorials you may like