将这串代码的输出图片分开保存为四个图像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) f.set_figheight(5) f.set_figwidth(20) plt.show()
时间: 2023-06-26 08:09:14 浏览: 70
可以使用以下代码将四个图像分别保存为文件:
```
import matplotlib.pyplot as plt
import numpy as np
# 代码中的内容
# 将图像保存为文件
plt.subplots_adjust(wspace=0.5)
for i in range(4):
axarr[i].title.set_fontsize(15)
axarr[i].axis('off')
plt.imshow(np.clip(axarr[i].get_images()[0], 0, 1))
plt.savefig(f"reconstructed_img_{i}.png")
```
这会将四个图像分别保存为`reconstructed_img_0.png`,`reconstructed_img_1.png`,`reconstructed_img_2.png`,`reconstructed_img_3.png`。注意,要在调用`imshow`之前添加`axarr[i].axis('off')`,以避免保存的图像包含坐标轴。另外,为了保留图像的质量,可以使用`np.clip`将像素值约束在0到1之间。
相关问题
f, axarr = plt.subplots(num_images_to_show,4)
这段代码使用 Matplotlib 库创建一个包含多个子图的图表,并将其存储在变量 f 中。其中,参数 num_images_to_show 指定了要显示的子图数量,每个子图都有 4 个轴(即左、右、上、下)。
变量 axarr 则是一个二维数组(即矩阵),用于存储每个子图的轴。通过使用 axarr[i,j] 的方式访问特定子图的轴,可以对其进行绘制操作。
fig, ax1 = plt.subplots(
fig, ax1 = plt.subplots()是用于创建一个包含一个子图的Figure对象和一个Axes对象的函数[^1]。其中,Figure对象代表整个图形窗口,而Axes对象则代表一个具体的绘图区域。
下面是一个示例,演示了如何使用fig, ax1 = plt.subplots()创建一个包含两个子图的图形窗口,并在每个子图中绘制不同的数据[^2]:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# 创建图形窗口和子图
fig, ax1 = plt.subplots()
# 在第一个子图中绘制数据
ax1.plot(x, y1, 'b', lw=1.5, label='1st')
ax1.plot(x, y1, 'ro')
ax1.grid(True)
ax1.legend(loc=0)
ax1.set_xlabel('index')
ax1.set_ylabel('value 1st')
ax1.set_title('A Simplot plot')
# 创建第二个子图
ax2 = ax1.twinx()
# 在第二个子图中绘制数据
ax2.plot(x, y2, 'g', lw=1.5, label='2nd')
ax2.plot(x, y2, 'ro')
ax2.legend(loc=0)
ax2.set_ylabel('value 2nd')
# 显示图形
plt.show()
```
这段代码会创建一个包含两个子图的图形窗口,其中第一个子图绘制了y1的数据,第二个子图绘制了y2的数据。每个子图都有自己的y轴,但共享x轴。在绘制完数据后,使用plt.show()函数显示图形。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](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)