plt.subplots()
时间: 2023-09-25 12:15:25 浏览: 92
The `plt.subplots()` function is a method that creates a figure object and a grid of subplots with a single call. It is a convenient way to create multiple subplots in one figure.
Syntax: `fig, ax = plt.subplots(nrows, ncols, ...)`
Parameters:
- `nrows`: number of rows of subplots
- `ncols`: number of columns of subplots
- `sharex`: share x-axis among subplots
- `sharey`: share y-axis among subplots
Returns:
- `fig`: a `Figure` object that represents the entire figure
- `ax`: a list of `Axes` objects representing all the subplots
Example:
```
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
ax[0, 0].plot([1, 2, 3], [4, 5, 6])
ax[0, 1].scatter([1, 2, 3], [4, 5, 6])
ax[1, 0].bar([1, 2, 3], [4, 5, 6])
ax[1, 1].hist([1, 2, 3, 4, 5, 6])
plt.show()
```
This will create a 2x2 grid of subplots with shared x-axis and y-axis. Each subplot will have a different type of plot.
阅读全文
相关推荐














