In this part of the Matplotlib series we’ll see how to add Axes instances to a figure.
To add an Axes instance, we use the add_axes method. When adding an Axes, we need to assign it to a region within the figure. To do that, we have to specify the coordinates of it’s bottom left corner and its size. The coordinates, width and height are proportional rather than absolute, so they are in the range between 0 and 1.
In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
# Let's create a figure with size 8 x 6 inches and set its background color.
fig = plt.figure(figsize=(8, 6), facecolor='yellow')
# Let's add three Axes instances:
ax1 = fig.add_axes((.1, .1, .5, .4), facecolor='cyan')
ax2 = fig.add_axes((.3, .6, .3, .3), facecolor='cyan')
ax3 = fig.add_axes((.7, .1, .2, .8), facecolor='cyan')
# Let's define the x and y values. We'll use the sine function from the previous lectures.
x = np.linspace(-10, 10, 100)
y = 10 * np.sin(x)
# Let's plot the sine function.
ax1.plot(x, y, color='green')
ax2.plot(x, y, color='green')
ax3.plot(x, y, color='green')
Out[1]:
Here’s the video version of this article: