如何在 Matplotlib 中在一个图形中显示多张图片?


要在 matplotlib 中在一个图形中显示多张图片,我们可以按照以下步骤操作:

  • 使用 numpy 创建随机数据。

  • 向当前图形添加子图,**nrows=1, ncols=4**,索引为 **index=1**。

  • 使用 **imshow()** 方法和 **cmap="Blues_r"** 将数据显示为图像(即二维规则栅格)。

  • 向当前图形添加子图,nrows=1, ncols=4,索引为index=2。

  • 使用 **imshow()** 方法和 **cmap="Accent_r"** 将数据显示为图像(即二维规则栅格)。

  • 向当前图形添加子图,nrows=1, ncols=4,索引为index=3。

  • 使用 **imshow()** 方法和 **cmap="terrain_r"** 将数据显示为图像(即二维规则栅格)。

  • 向当前图形添加子图,**nrows=1, ncols=4**,索引为 **index=4**。

  • 使用 imshow() 方法和 **cmap="twilight_shifted_r"** 将数据显示为图像(即二维规则栅格)。

  • 要显示图形,请使用 **show()** 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.rand(5, 5)
plt.subplot(1, 4, 1)
plt.imshow(data, cmap="Blues_r")
plt.subplot(1, 4, 2)
plt.imshow(data, cmap="Accent_r")
plt.subplot(1, 4, 3)
plt.imshow(data, cmap="terrain_r")
plt.subplot(1, 4, 4)
plt.imshow(data, cmap="twilight_shifted_r")
plt.show()

输出

更新于:2021年5月8日

7K+ 次浏览

启动你的 职业生涯

完成课程获得认证

开始学习
广告