Creating Matplotlib PathPatch Objects

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a Python library that allows the user to create different types of charts and graphs. It is used in data visualization and data analysis. In this lab, we will learn how to create a PathPatch object using Matplotlib's API.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} matplotlib/figures_axes -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} matplotlib/line_plots -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} matplotlib/grid_config -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} python/lists -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} python/tuples -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} python/importing_modules -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} python/data_visualization -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} python/build_in_functions -.-> lab-48854{{"`Creating Matplotlib PathPatch Objects`"}} end

Import Libraries

We need to import the required libraries for this lab.

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.path as mpath

Define Path Data

We define the path data in this step. Path data is a sequence of tuples that specify the vertices and codes of the path. We use the mpath.Path class to create a Path object from this data.

Path = mpath.Path
path_data = [
    (Path.MOVETO, (1.58, -2.57)),
    (Path.CURVE4, (0.35, -1.1)),
    (Path.CURVE4, (-1.75, 2.0)),
    (Path.CURVE4, (0.375, 2.0)),
    (Path.LINETO, (0.85, 1.15)),
    (Path.CURVE4, (2.2, 3.2)),
    (Path.CURVE4, (3, 0.05)),
    (Path.CURVE4, (2.0, -0.5)),
    (Path.CLOSEPOLY, (1.58, -2.57)),
    ]
codes, verts = zip(*path_data)
path = mpath.Path(verts, codes)

Create PathPatch Object

In this step, we create a PathPatch object using the path object that we created in the previous step. This object is used to fill the area enclosed by the path. We can also set the color and transparency of the patch.

patch = mpatches.PathPatch(path, facecolor='r', alpha=0.5)

Add PathPatch to Plot

Now, we add the patch object to the plot using the add_patch method of the axes object.

fig, ax = plt.subplots()
ax.add_patch(patch)

Plot Control Points and Connecting Lines

In this step, we plot the control points and connecting lines of the path using the plot method of the axes object.

x, y = zip(*path.vertices)
line, = ax.plot(x, y, 'go-')

Customize Plot

Finally, we customize the plot by adding a grid and equalizing the axes.

ax.grid()
ax.axis('equal')
plt.show()

Summary

In this lab, we learned how to create a PathPatch object using Matplotlib's API. We defined the path data, created a Path object, and used it to create a PathPatch object that we added to the plot. We also plotted the control points and connecting lines of the path and customized the plot by adding a grid and equalizing the axes.

Other Python Tutorials you may like