Filled Contour Plots with Hatching

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create filled contour plots with hatched patterns using Python's Matplotlib library. Contour plots are used to display three-dimensional data in two dimensions. They are particularly useful for visualizing data that has peaks and valleys, such as topographical data.

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.

Import Libraries

We begin by importing the necessary libraries. In this lab, we will be using NumPy and Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Create Data

Next, we will create some sample data to plot. In this example, we will create a 2D grid of x and y values and use them to calculate z values.

## invent some numbers, turning the x and y arrays into simple
## 2d arrays, which make combining them together easier.
x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)

Simplest Hatched Plot with a Colorbar

In this step, we will create the simplest hatched plot with a colorbar. We will use the contourf function to create the filled contour plot and specify the hatches using the hatches parameter.

fig1, ax1 = plt.subplots()
cs = ax1.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
                  cmap='gray', extend='both', alpha=0.5)
fig1.colorbar(cs)

Plot of Hatches without Color with a Legend

In this step, we will create a plot of hatches without color and add a legend. We will use the contour function to create the contour lines and the contourf function to specify the hatches without color.

fig2, ax2 = plt.subplots()
n_levels = 6
ax2.contour(x, y, z, n_levels, colors='black', linestyles='-')
cs = ax2.contourf(x, y, z, n_levels, colors='none',
                  hatches=['.', '/', '\\', None, '\\\\', '*'],
                  extend='lower')

## create a legend for the contour set
artists, labels = cs.legend_elements(str_format='{:2.1f}'.format)
ax2.legend(artists, labels, handleheight=2, framealpha=1)

Display the Plots

Finally, we will display the plots using the show function.

plt.show()

Summary

In this lab, we learned how to create filled contour plots with hatched patterns using Matplotlib. We used the contour and contourf functions to create the plots and specified the hatches using the hatches parameter. We also added a colorbar and legend to our plots.

Other Python Tutorials you may like