Subplots vs subplot in matplotlib: Key Differences and Usage
subplot creates a single plot in a grid by specifying its position, while subplots creates a full grid of plots and returns figure and axes objects for easier management. subplots is more flexible for multiple plots, whereas subplot is simpler for adding one plot at a time.Quick Comparison
Here is a quick side-by-side comparison of subplot and subplots in matplotlib.
| Feature | subplot | subplots |
|---|---|---|
| Purpose | Create one plot in a grid | Create multiple plots in a grid at once |
| Return Value | Single Axes object | Figure and array of Axes objects |
| Syntax Complexity | Simpler for single plot | More flexible for multiple plots |
| Usage Style | Add plots one by one | Create all plots together |
| Best For | Quick single plot in grid | Multiple plots with shared settings |
| Axes Management | Manual | Automatic and easier |
Key Differences
The subplot function is used to add a single plot to a figure by specifying the number of rows, columns, and the plot position. It returns one Axes object, which you can use to plot your data. This method is straightforward when you want to add plots one at a time.
On the other hand, subplots creates a full grid of plots in one call. It returns a Figure object and an array of Axes objects, which makes it easier to manage multiple plots together. This is especially useful when you want to customize or share properties across plots.
Using subplots is generally more modern and flexible. It supports features like shared axes and automatic layout adjustment, which subplot does not handle as smoothly. However, for very simple cases with just one plot, subplot can be quicker to write.
Code Comparison
Here is how you create a 2x2 grid of plots using subplot by adding each plot individually.
import matplotlib.pyplot as plt plt.figure(figsize=(8,6)) plt.subplot(2, 2, 1) plt.plot([1, 2, 3], [1, 4, 9]) plt.title('Plot 1') plt.subplot(2, 2, 2) plt.plot([1, 2, 3], [2, 3, 4]) plt.title('Plot 2') plt.subplot(2, 2, 3) plt.plot([1, 2, 3], [5, 6, 7]) plt.title('Plot 3') plt.subplot(2, 2, 4) plt.plot([1, 2, 3], [7, 8, 9]) plt.title('Plot 4') plt.tight_layout() plt.show()
subplots Equivalent
Here is how to create the same 2x2 grid of plots using subplots with easier axes management.
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2, figsize=(8,6)) axs[0, 0].plot([1, 2, 3], [1, 4, 9]) axs[0, 0].set_title('Plot 1') axs[0, 1].plot([1, 2, 3], [2, 3, 4]) axs[0, 1].set_title('Plot 2') axs[1, 0].plot([1, 2, 3], [5, 6, 7]) axs[1, 0].set_title('Plot 3') axs[1, 1].plot([1, 2, 3], [7, 8, 9]) axs[1, 1].set_title('Plot 4') plt.tight_layout() plt.show()
When to Use Which
Choose subplot when you want to quickly add a single plot to a figure and don't need to manage multiple axes at once. It is simple and direct for small tasks.
Choose subplots when you plan to create multiple plots together, want easier control over all axes, or need features like shared axes and automatic layout. It is the better choice for organized and scalable plotting.
Key Takeaways
subplot creates one plot at a time by position in a grid.subplots creates a full grid and returns figure and axes for easier control.subplots is more flexible and preferred for multiple plots.subplot for quick single plots, subplots for complex layouts.subplots supports shared axes and better layout management.