将两张图片以 f, axarr = plt.subplots(1,4) axarr[0].title.set_text('Original \n Image') axarr[1].title.set_text('Reconstructed Image with \n 43% Compression') axarr[2].title.set_text('Reconstructed Image with \n 68% Compression') axarr[3].title.set_text('Reconstructed Image with \n 84% Compression') for i in range(4): axarr[i].title.set_fontsize(15) axarr[0].imshow((valid_batch[0].cpu().detach().permute(1, 2, 0) * 0.5) + 0.5) axarr[1].imshow((reconstructed_img_28[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5) axarr[2].imshow((reconstructed_img_16[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5) axarr[3].imshow((reconstructed_img_8[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5)这种形式显示
时间: 2024-02-20 11:02:31 浏览: 21
这个代码可以使用Matplotlib库来绘制四张图片,每张图片都有标题和子标题。以下是代码示例:
```python
import matplotlib.pyplot as plt
import torch
# 获取图像数据
valid_batch = <your original image data>
reconstructed_img_28 = <your reconstructed image data with 43% compression>
reconstructed_img_16 = <your reconstructed image data with 68% compression>
reconstructed_img_8 = <your reconstructed image data with 84% compression>
# 创建一个1x4的子图
f, axarr = plt.subplots(1, 4)
# 设置子图标题
axarr[0].title.set_text('Original \n Image')
axarr[1].title.set_text('Reconstructed Image with \n 43% Compression')
axarr[2].title.set_text('Reconstructed Image with \n 68% Compression')
axarr[3].title.set_text('Reconstructed Image with \n 84% Compression')
# 设置子标题字体大小
for i in range(4):
axarr[i].title.set_fontsize(15)
# 在每个子图中显示图像
axarr[0].imshow((valid_batch[0].cpu().detach().permute(1, 2, 0) * 0.5) + 0.5)
axarr[1].imshow((reconstructed_img_28[0].cpu().detach().permute(1, 2, 0) * 0.5) + 0.5)
axarr[2].imshow((reconstructed_img_16[0].cpu().detach().permute(1, 2, 0) * 0.5) + 0.5)
axarr[3].imshow((reconstructed_img_8[0].cpu().detach().permute(1, 2, 0) * 0.5) + 0.5)
# 显示图像
plt.show()
```
确保在代码中替换 `<your original image data>`、`<your reconstructed image data with 43% compression>`、`<your reconstructed image data with 68% compression>` 和 `<your reconstructed image data with 84% compression>` 部分的数据。如果需要,您可以修改标题和子标题的文本和字体大小。