Pcolormesh Grids and Shading

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a data visualization library for Python. It provides a variety of tools for creating static, animated, and interactive visualizations in Python. In this lab, we will learn how to use pcolormesh and pcolor functions in Matplotlib to visualize 2D grids.

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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48859{{"`Pcolormesh Grids and Shading`"}} matplotlib/figures_axes -.-> lab-48859{{"`Pcolormesh Grids and Shading`"}} python/lists -.-> lab-48859{{"`Pcolormesh Grids and Shading`"}} python/tuples -.-> lab-48859{{"`Pcolormesh Grids and Shading`"}} python/importing_modules -.-> lab-48859{{"`Pcolormesh Grids and Shading`"}} python/numerical_computing -.-> lab-48859{{"`Pcolormesh Grids and Shading`"}} python/data_visualization -.-> lab-48859{{"`Pcolormesh Grids and Shading`"}} end

Import Required Libraries

First, we need to import the required libraries, Matplotlib and NumPy, by running the following code block:

import matplotlib.pyplot as plt
import numpy as np

Create Data for Visualization

Next, we will create a 2D grid that we will use for visualization. We can create a grid using the meshgrid function in NumPy. The meshgrid function creates a grid of points given two vectors, x and y, which represent the coordinates of the grid points. We will create a grid of 5x5 points using the following code block:

nrows = 5
ncols = 5
x = np.arange(ncols + 1)
y = np.arange(nrows + 1)
X, Y = np.meshgrid(x, y)
Z = X + Y

Flat Shading

The pcolormesh function in Matplotlib can visualize 2D grids. The grid specification with the least assumptions is shading='flat' and if the grid is one larger than the data in each dimension, i.e., has shape (M+1, N+1). In that case, X and Y specify the corners of quadrilaterals that are colored with the values in Z. We can visualize the grid using the following code block:

fig, ax = plt.subplots()
ax.pcolormesh(X, Y, Z, shading='flat', cmap='viridis')
ax.set_title('Flat Shading')
plt.show()

Flat Shading, Same Shape Grid

If the grid is the same shape as the data in each dimension, we cannot use shading='flat'. Historically, Matplotlib silently dropped the last row and column of Z in this case, to match Matlab's behavior. If this behavior is still desired, simply drop the last row and column manually. We can visualize the grid using the following code block:

fig, ax = plt.subplots()
ax.pcolormesh(x, y, Z[:-1, :-1], shading='flat', cmap='viridis')
ax.set_title('Flat Shading, Same Shape Grid')
plt.show()

Nearest Shading, Same Shape Grid

Usually, dropping a row and column of data is not what the user means when they make X, Y, and Z all the same shape. For this case, Matplotlib allows shading='nearest' and centers the colored quadrilaterals on the grid points. If a grid that is not the correct shape is passed with shading='nearest', an error is raised. We can visualize the grid using the following code block:

fig, ax = plt.subplots()
ax.pcolormesh(X, Y, Z, shading='nearest', cmap='viridis')
ax.set_title('Nearest Shading, Same Shape Grid')
plt.show()

Auto Shading

It's possible that the user would like the code to automatically choose which to use, in this case, shading='auto' will decide whether to use flat or nearest shading based on the shapes of X, Y, and Z. We can visualize the grid using the following code block:

fig, ax = plt.subplots()
ax.pcolormesh(x, y, Z, shading='auto', cmap='viridis')
ax.set_title('Auto Shading')
plt.show()

Gouraud Shading

Gouraud shading can also be specified, where the color in the quadrilaterals is linearly interpolated between the grid points. The shapes of X, Y, Z must be the same. We can visualize the grid using the following code block:

fig, ax = plt.subplots()
ax.pcolormesh(X, Y, Z, shading='gouraud', cmap='viridis')
ax.set_title('Gouraud Shading')
plt.show()

Summary

In this lab, we learned how to use pcolormesh and pcolor functions in Matplotlib to visualize 2D grids. We learned about different shading options, including flat, nearest, auto, and gouraud. We also learned how to create a 2D grid using the meshgrid function in NumPy.

Other Python Tutorials you may like