首页 > 解决方案 > Python:将三个图像合二为一地显示前后

问题描述

我正在尝试制作如下图所示的图像,该图像将由 3 个大小相同的数组组成,这些数组仅部分显示。有没有办法对三个数组进行切片或重叠绘制以获得像这样的划分?

在此处输入图像描述

标签: pythonmatplotlib

解决方案


如何使用补丁

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.lines as lines
import os

patch1 = ((0,0.3),(0,1),(0.5,1),(0.5,0.5))
patch2 = ((0.5,0.5),(0.5,1),(1,1),(1,0.3))
patch3 = ((0,0),(0,0.3),(0.5,0.5),(1,0.3),(1,0))

# Pictures from Win10 in WSL2
path = r"/mnt/c/Windows/Web/Wallpaper/Theme1"

img1 = plt.imread(os.path.join(path, "img1.jpg"))
img2 = plt.imread(os.path.join(path, "img2.jpg"))
img3 = plt.imread(os.path.join(path, "img3.jpg"))


fig, ax = plt.subplots()

poly1 = patches.Polygon(patch1, transform=ax.transAxes)
poly2 = patches.Polygon(patch2, transform=ax.transAxes)
poly3 = patches.Polygon(patch3, transform=ax.transAxes)

ip1 = ax.imshow(img1)
ip2 = ax.imshow(img2)
ip3 = ax.imshow(img3)

ip1.set_clip_path(poly1)
ip2.set_clip_path(poly2)
ip3.set_clip_path(poly3)

l1 = lines.Line2D((0, 0.5), (0.3, 0.5), color="w", transform=ax.transAxes)
l2 = lines.Line2D((0.5, 0.5), (0.5, 1), color="w", transform=ax.transAxes)
l3 = lines.Line2D((0.5, 1), (0.5, 0.3), color="w", transform=ax.transAxes)

l1.set_linewidth(5)
l2.set_linewidth(5)
l3.set_linewidth(5)

fig.add_artist(l1)
fig.add_artist(l2)
fig.add_artist(l3)


ax.axis('off')
plt.show()

推荐阅读