matplotlib.pyplot.subplot(nrows, ncols, plot_number)详解
时间: 2024-06-09 19:04:46 浏览: 169
`matplotlib.pyplot.subplot(nrows, ncols, plot_number)` 是 `matplotlib` 库中的一个函数,用于在一个窗口中创建多个子图。它接受三个参数:
- `nrows`:表示子图的行数
- `ncols`:表示子图的列数
- `plot_number`:表示当前子图的编号
函数返回当前子图的 `Axes` 对象。
如果 `nrows`、`ncols` 和 `plot_number` 都小于 10,可以将它们写成一个整数,例如 `plt.subplot(223)` 表示将当前窗口分成 2 行 2 列,当前子图的编号为 3。
下面是一个例子:
```python
import matplotlib.pyplot as plt
# 创建一个 2 行 2 列的窗口,并将当前子图设置为第一个
plt.subplot(2, 2, 1)
plt.plot([1, 2, 3], [1, 2, 3])
plt.title('Subplot 1')
# 将当前子图设置为第二个
plt.subplot(2, 2, 2)
plt.plot([1, 2, 3], [3, 2, 1])
plt.title('Subplot 2')
# 将当前子图设置为第三个
plt.subplot(2, 2, 3)
plt.plot([1, 2, 3], [2, 3, 1])
plt.title('Subplot 3')
# 将当前子图设置为第四个
plt.subplot(2, 2, 4)
plt.plot([1, 2, 3], [1, 3, 2])
plt.title('Subplot 4')
plt.show()
```
运行结果:

阅读全文
相关推荐

















