Hatch Style Reference

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through the process of adding hatches to most polygons in Matplotlib, including Axes.bar, Axes.fill_between, Axes.contourf, and children of patches.Polygon. You will learn how to create different hatching patterns, repeat them to increase density, and combine them to create additional patterns.

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/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) 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/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48764{{"`Hatch Style Reference`"}} matplotlib/figures_axes -.-> lab-48764{{"`Hatch Style Reference`"}} python/booleans -.-> lab-48764{{"`Hatch Style Reference`"}} python/for_loops -.-> lab-48764{{"`Hatch Style Reference`"}} python/lists -.-> lab-48764{{"`Hatch Style Reference`"}} python/tuples -.-> lab-48764{{"`Hatch Style Reference`"}} python/sets -.-> lab-48764{{"`Hatch Style Reference`"}} python/function_definition -.-> lab-48764{{"`Hatch Style Reference`"}} python/importing_modules -.-> lab-48764{{"`Hatch Style Reference`"}} python/using_packages -.-> lab-48764{{"`Hatch Style Reference`"}} python/data_visualization -.-> lab-48764{{"`Hatch Style Reference`"}} python/build_in_functions -.-> lab-48764{{"`Hatch Style Reference`"}} end

Import the necessary libraries

To use Matplotlib, we must import the library and the necessary modules. We will use the pyplot module to create the subplots and the patches module to create the Rectangle object.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

Create the hatches_plot function

The hatches_plot function will create a rectangle with the specified hatching pattern and add it to the axis. It will also add a text with the hatching pattern.

def hatches_plot(ax, h):
    ax.add_patch(Rectangle((0, 0), 2, 2, fill=False, hatch=h))
    ax.text(1, -0.5, f"' {h} '", size=15, ha="center")
    ax.axis('equal')
    ax.axis('off')

Create the subplots

We will create three sets of subplots with different hatching patterns.

fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))

Create the first set of hatching patterns

We will create the first set of hatching patterns using the following list:

hatches = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']

We will then use a loop to create a rectangle with each hatching pattern and add it to a subplot.

for ax, h in zip(axs.flat, hatches):
    hatches_plot(ax, h)

Create the second set of hatching patterns

We will create the second set of hatching patterns by repeating each pattern twice to increase the density. We will use the following list:

hatches = ['//', '\\\\', '||', '--', '++', 'xx', 'oo', 'OO', '..', '**']

We will use the same loop as before to create the rectangles.

for ax, h in zip(axs.flat, hatches):
    hatches_plot(ax, h)

Create the third set of hatching patterns

We will create the third set of hatching patterns by combining two patterns to create a new one. We will use the following list:

hatches = ['/o', '\\|', '|*', '-\\', '+o', 'x*', 'o-', 'O|', 'O.', '*-']

We will use the same loop as before to create the rectangles.

for ax, h in zip(axs.flat, hatches):
    hatches_plot(ax, h)

Display the plots

We will display the plots using the show() function.

plt.show()

Summary

In this lab, you learned how to add hatches to most polygons in Matplotlib, including Axes.bar, Axes.fill_between, Axes.contourf, and children of patches.Polygon. You learned how to create different hatching patterns, repeat them to increase density, and combine them to create additional patterns. You also learned how to use the add_patch() function to add a Rectangle object to an axis and how to use the text() function to add text to the plot.

Other Python Tutorials you may like