Building your own polygons

You can also make your own polygons by specifying where those vertices lie. In the following code, we can see that there are four vertices corresponding to a trapezoid, as follows:

# Making your own polygons
pos = [(3,0),(7,0), (6,1), (4,1)]
poly = mpl.patches.Polygon(pos)
plt.plot(nums, 10/3.*np.sin(nums))
plt.gca().add_patch(poly)

When we run this code, we get a trapezoid, as shown here:

Matplotlib provides the ability to build much more complicated and sophisticated shapes using paths. It is recommended to take a look at the matplotlib.path module. In the path module, we can see that there is a module available for dealing with multiple different kinds of lines. By combining these different lines—straight lines, Bezier curves, and other lines—we can build any kind of shape that we want:

mpl.path?

We will get the following screen:

In fact, Matplotlib provides a language similar to SVG, where we can build sophisticated vector graphics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset