Creating figures and subplots
Matplotlib supports plotting multiple charts (subplots) on a single figure, which is Matplotlib's term for the drawing canvas.
Defining figures' subplots
To create a matplotlib.pyplot.figure
object, use the following method:
import matplotlib.pyplot as plt fig = plt.figure(figsize=(12, 6), dpi=200)
This yields an empty figure object (0 Axes
):
<Figure size 2400x1200 with 0 Axes>
Before we plot anything on this figure, we need to add subplots to create space for them. The matplotlib.pyplot.figure.add_subplot(...)
method lets us do that by specifying the size of the subplot and the location.
The following code block adds a subplot of size 1x2 grids on the left, then a subplot of 2x2 on the top right, and finally, a subplot of 2x2 on the bottom right:
ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 4) fig
The result is the following figure object containing the subplots we...