Matplotlib Pcolor Visualization Tutorial

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial is an introduction to using Pcolor in Matplotlib. Pcolor allows you to generate 2D image-style plots, and we will show you how to use it in Matplotlib.

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/PlottingDataGroup(["`Plotting Data`"]) 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/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} python/with_statement -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} matplotlib/heatmaps -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} python/for_loops -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} python/lists -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} python/tuples -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} python/standard_libraries -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} python/math_random -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} python/build_in_functions -.-> lab-48858{{"`Matplotlib Pcolor Visualization Tutorial`"}} end

Simple Pcolor Demo

The first step is to create a simple Pcolor demo. This will show you how to create a basic Pcolor plot.

Z = np.random.rand(6, 10)

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

c = ax0.pcolor(Z)
ax0.set_title('default: no edges')

c = ax1.pcolor(Z, edgecolors='k', linewidths=4)
ax1.set_title('thick edges')

fig.tight_layout()
plt.show()

Comparing Pcolor with Similar Functions

The second step is to compare Pcolor with similar functions, such as Pcolormesh, Imshow and Pcolorfast. This will help you understand the differences between these functions and when to use each one.

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

## generate 2 2d grids for the x & y bounds
y, x = np.mgrid[-3:3+dy:dy, -3:3+dx:dx]
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
## 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]
z_min, z_max = -abs(z).max(), abs(z).max()

fig, axs = plt.subplots(2, 2)

ax = axs[0, 0]
c = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_title('pcolor')
fig.colorbar(c, ax=ax)

ax = axs[0, 1]
c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_title('pcolormesh')
fig.colorbar(c, ax=ax)

ax = axs[1, 0]
c = ax.imshow(z, cmap='RdBu', vmin=z_min, vmax=z_max,
              extent=[x.min(), x.max(), y.min(), y.max()],
              interpolation='nearest', origin='lower', aspect='auto')
ax.set_title('image (nearest, aspect="auto")')
fig.colorbar(c, ax=ax)

ax = axs[1, 1]
c = ax.pcolorfast(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
ax.set_title('pcolorfast')
fig.colorbar(c, ax=ax)

fig.tight_layout()
plt.show()

Pcolor with a Log Scale

The third step is to create a Pcolor plot with a log scale. This is useful when you have data with a wide range of values.

N = 100
X, Y = np.meshgrid(np.linspace(-3, 3, N), np.linspace(-2, 2, N))

## A low hump with a spike coming out.
## Needs to have z/colour axis on a log scale, so we see both hump and spike.
## A linear scale only shows the spike.
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
Z = Z1 + 50 * Z2

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

c = ax0.pcolor(X, Y, Z, shading='auto',
               norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='PuBu_r')
fig.colorbar(c, ax=ax0)

c = ax1.pcolor(X, Y, Z, cmap='PuBu_r', shading='auto')
fig.colorbar(c, ax=ax1)

plt.show()

Summary

This tutorial has shown you how to use Pcolor in Matplotlib. We started with a simple Pcolor demo, then compared Pcolor with similar functions like Pcolormesh and Imshow. Finally, we created a Pcolor plot with a log scale.

Other Python Tutorials you may like