plt.subplots的gridspec_kw控制10个子图
时间: 2023-08-28 11:10:44 浏览: 69
是的,你可以使用 `plt.subplots()` 函数来创建一个包含多个子图的图形,并且可以使用 `gridspec_kw` 参数来控制每个子图的位置和大小。例如,如果你想要创建一个包含10个子图的图形,你可以使用以下代码:
```python
import matplotlib.pyplot as plt
fig, axs = plt.subplots(5, 2, figsize=(10, 10), gridspec_kw={'hspace': 0.4})
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
axs[0, 0].set_title('Subplot 1')
axs[0, 1].plot([1, 2, 3], [6, 5, 4])
axs[0, 1].set_title('Subplot 2')
axs[1, 0].plot([1, 2, 3], [1, 2, 3])
axs[1, 0].set_title('Subplot 3')
axs[1, 1].plot([1, 2, 3], [3, 2, 1])
axs[1, 1].set_title('Subplot 4')
axs[2, 0].plot([1, 2, 3], [2, 4, 6])
axs[2, 0].set_title('Subplot 5')
axs[2, 1].plot([1, 2, 3], [6, 4, 2])
axs[2, 1].set_title('Subplot 6')
axs[3, 0].plot([1, 2, 3], [5, 5, 5])
axs[3, 0].set_title('Subplot 7')
axs[3, 1].plot([1, 2, 3], [4, 4, 4])
axs[3, 1].set_title('Subplot 8')
axs[4, 0].plot([1, 2, 3], [1, 4, 9])
axs[4, 0].set_title('Subplot 9')
axs[4, 1].plot([1, 2, 3], [9, 4, 1])
axs[4, 1].set_title('Subplot 10')
plt.show()
```
这将创建一个包含 5 行和 2 列的子图矩阵,每个子图都有一个标题,并且它们之间的间距为 0.4。你可以根据需要更改 `gridspec_kw` 参数来控制子图的位置和大小。