subplot函数怎么用
时间: 2023-07-08 21:26:45 浏览: 35
subplot函数是Matplotlib库中用于创建子图的函数,它的语法格式如下:
```python
subplot(nrows, ncols, plot_number)
```
其中,nrows和ncols分别指定子图的行数和列数,plot_number则指定当前子图的编号。例如,如果要创建一个2行3列的子图,可以使用以下代码:
```python
import matplotlib.pyplot as plt
# 创建一个2行3列的子图
fig, axs = plt.subplots(nrows=2, ncols=3)
# 绘制第1个子图
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
axs[0, 0].set_title('Subplot 1')
# 绘制第2个子图
axs[0, 1].scatter([1, 2, 3], [4, 5, 6])
axs[0, 1].set_title('Subplot 2')
# 绘制第3个子图
axs[0, 2].bar([1, 2, 3], [4, 5, 6])
axs[0, 2].set_title('Subplot 3')
# 绘制第4个子图
axs[1, 0].pie([1, 2, 3])
axs[1, 0].set_title('Subplot 4')
# 绘制第5个子图
axs[1, 1].imshow([[1, 2], [3, 4]])
axs[1, 1].set_title('Subplot 5')
# 绘制第6个子图
axs[1, 2].plot([1, 2, 3], [6, 5, 4])
axs[1, 2].set_title('Subplot 6')
plt.show()
```
在这个例子中,我们创建了一个2行3列的子图,对每个子图进行了绘制,并使用set_title方法设置了每个子图的标题。最后,使用plt.show()方法显示整个图形。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)