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.
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.