Creating a Compound Path

PythonPythonBeginner
Practice Now

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

Introduction

In this tutorial, we will learn how to create a compound path using Matplotlib in Python. A compound path is a collection of simple paths that can be used to create complex shapes. We will create a compound path by combining two simple polygons, a rectangle, and a triangle.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/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`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48614{{"`Creating a Compound Path`"}} matplotlib/importing_matplotlib -.-> lab-48614{{"`Creating a Compound Path`"}} matplotlib/figures_axes -.-> lab-48614{{"`Creating a Compound Path`"}} python/lists -.-> lab-48614{{"`Creating a Compound Path`"}} python/tuples -.-> lab-48614{{"`Creating a Compound Path`"}} python/importing_modules -.-> lab-48614{{"`Creating a Compound Path`"}} python/using_packages -.-> lab-48614{{"`Creating a Compound Path`"}} python/numerical_computing -.-> lab-48614{{"`Creating a Compound Path`"}} python/data_visualization -.-> lab-48614{{"`Creating a Compound Path`"}} end

Importing the Required Libraries

We will start by importing the required libraries. We need matplotlib.pyplot for creating the plot, matplotlib.patches for creating patches, matplotlib.path.Path for creating paths, and numpy for creating arrays.

import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path
import numpy as np

Creating the Vertices and Codes

We will create the vertices and codes for the two polygons that we want to combine into a compound path. We will use Path.MOVETO to move the cursor to the starting point of the polygon, Path.LINETO to create a line from the starting point to the next point, and Path.CLOSEPOLY to close the polygon.

vertices = []
codes = []

## First Polygon - Rectangle
codes = [Path.MOVETO] + [Path.LINETO]*3 + [Path.CLOSEPOLY]
vertices = [(1, 1), (1, 2), (2, 2), (2, 1), (0, 0)]

## Second Polygon - Triangle
codes += [Path.MOVETO] + [Path.LINETO]*2 + [Path.CLOSEPOLY]
vertices += [(4, 4), (5, 5), (5, 4), (0, 0)]

Creating the Path

We will use Path to create the path from the vertices and codes that we created in the previous step.

path = Path(vertices, codes)

Creating the PathPatch

We will create a PathPatch from the path that we created in the previous step. We will set the facecolor to 'none' and the edgecolor to 'green'.

pathpatch = PathPatch(path, facecolor='none', edgecolor='green')

Creating the Plot

We will create the plot and add the PathPatch to the plot. We will set the title of the plot to 'A Compound Path'.

fig, ax = plt.subplots()
ax.add_patch(pathpatch)
ax.set_title('A Compound Path')

ax.autoscale_view()

plt.show()

Summary

In this tutorial, we learned how to create a compound path using Matplotlib in Python. We created a compound path by combining two simple polygons, a rectangle, and a triangle. We used Path to create the path from the vertices and codes, and PathPatch to create a patch from the path. Finally, we added the patch to the plot to display the compound path.

Other Python Tutorials you may like