python,读取图片后,使用matplotlib库以4*4网格的形式绘制16张图片,给出代码演示
时间: 2023-12-19 13:05:48 浏览: 60
好的,下面是一个简单的代码演示:
```python
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# 读取16张图片
img1 = mpimg.imread('img1.jpg')
img2 = mpimg.imread('img2.jpg')
img3 = mpimg.imread('img3.jpg')
img4 = mpimg.imread('img4.jpg')
img5 = mpimg.imread('img5.jpg')
img6 = mpimg.imread('img6.jpg')
img7 = mpimg.imread('img7.jpg')
img8 = mpimg.imread('img8.jpg')
img9 = mpimg.imread('img9.jpg')
img10 = mpimg.imread('img10.jpg')
img11 = mpimg.imread('img11.jpg')
img12 = mpimg.imread('img12.jpg')
img13 = mpimg.imread('img13.jpg')
img14 = mpimg.imread('img14.jpg')
img15 = mpimg.imread('img15.jpg')
img16 = mpimg.imread('img16.jpg')
# 创建一个4x4的网格
fig, axs = plt.subplots(4, 4)
# 将每个图片放入对应的网格中
axs[0, 0].imshow(img1)
axs[0, 1].imshow(img2)
axs[0, 2].imshow(img3)
axs[0, 3].imshow(img4)
axs[1, 0].imshow(img5)
axs[1, 1].imshow(img6)
axs[1, 2].imshow(img7)
axs[1, 3].imshow(img8)
axs[2, 0].imshow(img9)
axs[2, 1].imshow(img10)
axs[2, 2].imshow(img11)
axs[2, 3].imshow(img12)
axs[3, 0].imshow(img13)
axs[3, 1].imshow(img14)
axs[3, 2].imshow(img15)
axs[3, 3].imshow(img16)
# 隐藏坐标轴
for ax in axs.flat:
ax.axis('off')
# 显示图片
plt.show()
```
需要注意的是,要使用`matplotlib.pyplot.imshow()`函数来显示图片,而读取图片则需要使用`matplotlib.image.imread()`函数。在创建网格时,可以使用`subplots()`函数来创建,然后使用二维数组的索引来访问每个网格。最后,可以使用`axis('off')`方法隐藏坐标轴。
阅读全文