Adding polygons and shapes to our plots

We will begin by importing what we need to from Matplotlib, as shown here:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))

We will use the same sign plot as in the earlier section, Adding text to both axis and figure objects. As shown in the following output, this is the most basic sine plot and no annotations have been added yet:

Begin by adding a circle in-between them. Define the circle using the Matplotlib patches module, as shown here:

# Circle
circ = mpl.patches.Circle((5,0), radius=1)
plt.plot(nums, np.sin(nums))
plt.gca().add_patch(circ)

We get a circle with a radius of one, as shown here:

The preceding figure does not look exactly like a circle; it looks like an ellipse that has been stretched—this is because our axis does not have an equal aspect ratio, even though it ranges from + 1 to -1.

To get the aspect ratio to be equal, we use the set_aspect method on the axis objects. If we set this to be equal, the x and y coordinates actually map an equal ratio to the data or to the size of the figure itself, so that we get a much longer looking plot, but our circle actually becomes circular, as follows:

# Circle
circ = mpl.patches.Circle((5,0), radius=1)
plt.plot(nums, np.sin(nums))
plt.gca().add_patch(circ)
plt.gca().set_aspect('equal')

We will get the following output:

If you're wondering why the polygons and the shapes that you've added to your plot don't look exactly like you expect them to, keep in mind that the aspect ratios may not always be equal by default.

We can add two patches to a plot by performing the following function:

# zorder
c1 = mpl.patches.Circle((5,0), radius=1)
c2 = mpl.patches.Circle((4,0), radius=1, color='r')
plt.plot(nums, 10/3.*np.sin(nums))
plt.gca().add_patch(c1)
plt.gca().add_patch(c2)

We will get the following output:

In the preceding figure, we have two circles; the above circle is the one that was added last to the axis, and the circle that's below is the one that was added first.

When we swap the order (for specification you can add colors to the circles to distinguish them) we will see that the circles will be overlapped, as shown:

# zorder
c1 = mpl.patches.Circle((5,0), radius=1)
c2 = mpl.patches.Circle((4,0), radius=1, color='r')
plt.plot(nums, 10/3.*np.sin(nums))
plt.gca().add_patch(c2)
plt.gca().add_patch(c1)

The output can be shown as:

But if you have multiple patches, or multiple shapes, or polygons, it is not necessary to overlap them simply based on the order they're called in. To set this manually, we can use the zorder argument. The object with the highest zorder is the one that is displayed last â€“ it's the one that ends up overlapping all of the rest.

So, if we set the first one to have a zorder of 0 and the second one to have a zorder of 1, the second one will be the one on top, as shown here:

# zorder
c1 = mpl.patches.Circle((5,0), radius=1, zorder=0)
c2 = mpl.patches.Circle((4,0), radius=1, zorder=1, color='r')

We get the following screenshot:

If the zorders are swapped, in which the second one has the zorder of 0 and the first one has the zorder of 1, the first one will be the one on top, as shown here:

# zorder
c1 = mpl.patches.Circle((5,0), radius=1, zorder=1)
c2 = mpl.patches.Circle((4,0), radius=1, zorder=0, color='r')
plt.plot(nums, 10/3.*np.sin(nums))
plt.gca().add_patch(c2)
plt.gca().add_patch(c1)

Hence, whichever patch has the highest zorder will be the one that is drawn last. So, if you want to set what covers other patches, keep in mind that the zorder is the best way of doing this; otherwise, we can simply use the order in which they are added to the plot.

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

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