Matplotlib Hatch Textures for Plots

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to use hatches in Python Matplotlib to add texture to your plots. Hatches are patterns that are used to fill the area of a plot. You can use hatches to differentiate between different parts of your plot or to add visual interest to your plot.

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/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/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/PlottingDataGroup -.-> matplotlib/fill_between("`Fill Between Plots`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/bar_charts("`Bar Charts`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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/fill_between -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} matplotlib/importing_matplotlib -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} matplotlib/figures_axes -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} matplotlib/bar_charts -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} python/booleans -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} python/lists -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} python/tuples -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} python/importing_modules -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} python/numerical_computing -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} python/data_visualization -.-> lab-48763{{"`Matplotlib Hatch Textures for Plots`"}} end

Import Libraries

To get started, you need to import the necessary libraries. In this case, we will be using Matplotlib and NumPy. NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

import matplotlib.pyplot as plt
import numpy as np

Create Data

Next, you need to create some data to use in your plot. In this case, we will be creating two arrays using the NumPy library.

x = np.arange(1, 5)
y1 = np.arange(1, 5)
y2 = np.ones(y1.shape) * 4

Create a Bar Plot with Hatching

Now that you have your data, you can create a bar plot with hatching. You can use hatching to create patterns on the bars in your plot. In this case, we will be using the hatch parameter to add hatching to our bars.

plt.bar(x, y1, edgecolor='black', hatch="/")
plt.bar(x, y2, bottom=y1, edgecolor='black', hatch='//')

Create a Bar Plot with Multiple Hatches

You can also use multiple hatches in your bar plot. In this case, we will be using an array of hatches to create multiple hatches on our bars.

plt.bar(x, y1, edgecolor='black', hatch=['--', '+', 'x', '\\'])
plt.bar(x, y2, bottom=y1, edgecolor='black', hatch=['*', 'o', 'O', '.'])

Create a Plot with Hatched Patches

You can also use hatching with patches in your plot. In this case, we will be using the fill_between function to create a hatched patch.

x = np.arange(0, 40, 0.2)
plt.fill_between(x, np.sin(x) * 4 + 30, y2=0, hatch='///', zorder=2, fc='c')

Add an Ellipse Patch with Hatching

You can also add hatched patches to your plot. In this case, we will be using the add_patch function to add an ellipse patch to our plot.

plt.gca().add_patch(Ellipse((4, 50), 10, 10, fill=True, hatch='*', facecolor='y'))

Add a Polygon Patch with Hatching

You can also add a polygon patch with hatching. In this case, we will be using the add_patch function to add a polygon patch to our plot.

plt.gca().add_patch(Polygon([(10, 20), (30, 50), (50, 10)], hatch='\\/...', facecolor='g'))

Set Plot Limits and Aspect Ratio

Finally, you can set the limits and aspect ratio of your plot to ensure that it looks the way you want it to.

plt.xlim([0, 40])
plt.ylim([10, 60])
plt.gca().set_aspect(1)

Summary

In this lab, you learned how to use hatches in Python Matplotlib to add texture to your plots. You learned how to create a bar plot with hatching, a bar plot with multiple hatches, a plot with hatched patches, an ellipse patch with hatching, a polygon patch with hatching, and how to set the limits and aspect ratio of your plot.

Other Python Tutorials you may like