fig.add_subplot
时间: 2023-09-24 19:09:45 浏览: 111
The `add_subplot()` method is used to add a new subplot to a Figure.
Syntax: `fig.add_subplot(nrows, ncols, index, **kwargs)`
- `nrows`: the number of rows of subplots in the grid
- `ncols`: the number of columns of subplots in the grid
- `index`: the index of the subplot to be created
- `**kwargs`: additional parameters that can be passed to customize the subplot (such as `title`, `xlabel`, `ylabel`, etc.)
Example usage:
```
import matplotlib.pyplot as plt
fig = plt.figure()
# adding a 2x2 grid of subplots
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
# customizing the subplots
ax1.set_title('Subplot 1')
ax2.set_xlabel('X Label')
ax3.set_ylabel('Y Label')
ax4.set_xlim([0, 10])
plt.show()
```
This code creates a Figure with a 2x2 grid of subplots, and then customizes each subplot by setting a title, xlabel, ylabel, and xlim.
阅读全文