python 如何在遮罩的边缘周围画一条线?

prdp8dxp  于 2023-06-04  发布在  Python
关注(0)|答案(3)|浏览(172)

我用面具做了一个热图。这里是一个玩具MWE:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

images = []
vmin = 0
vmax = 80
cmap = "viridis"
size = 40
matrix = np.random.randint(vmin, vmax, size=(size,size))
np.random.seed(7)
mask = []
for _ in range(size):
    prefix_length = np.random.randint(size)
    mask.append([False]*prefix_length + [True]*(size-prefix_length))
mask = np.array(mask)
sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap="viridis", mask=mask)
plt.savefig("temp.png")
plt.show()

我想在面具的边缘画一条线来强调它的位置。你怎么能这么做?
我的玩具示例目前看起来像这样:

koaltpgm

koaltpgm1#

fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap="viridis", mask=mask, ax=ax)

# get the ytick locations, which corresponds to the middle of each row
y = ax.get_yticks()

# get the edges of the squares
y_min = y - 0.5
y_max = y + 0.5

# find the index of the first instance of True
row_max = mask.argmax(axis=1)

# plot the vertical lines
ax.vlines(x=row_max, ymin=y_min, ymax=y_max, colors='r', lw=1.5)

# plot the horizontal lines
ax.hlines(y=y_max[:-1], xmin=row_max[:-1], xmax=row_max[1:], colors='r', lw=1.5)

plt.show()

wh6knrhe

wh6knrhe2#

在边缘周围绘制一条线的示例

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from skimage import measure

size = 10
matrix = np.random.randint(0, 80, size=(size, size))
np.random.seed(7)

mask = []
for _ in range(size):
    prefix_length = np.random.randint(size)
    mask.append([False] * prefix_length + [True] * (size - prefix_length))
mask = np.array(mask)

sns.heatmap(matrix, vmin=0, vmax=80, cmap="viridis", mask=mask)

# Find and shift the contour of the masked region
contour = measure.find_contours(image=mask, level=0.5)[0]
shift = 0.5
contour[:, 1] += shift
contour[:, 0] += shift

# Find and add additional points to improve the contour plot
points = []
for idx0 in range(len(contour)):
    pos0 = contour[idx0]
    idx1 = idx0 + 1
    if len(contour) > idx1:
        pos1 = contour[idx1]
        y1, x1 = tuple(pos1)
        y0, x0 = tuple(pos0)
        if y1 - y0 and x1 - x0:
            point = np.array([y0, x1])
            if y0 % 1:
                point = np.array([y1, x0])
            points.append((idx0, point))
points.reverse()
for idx0, point in points:
    contour = np.insert(contour, idx0 + 1, point, axis=0)

# Plot the contour
plt.plot(contour[:, 1], contour[:, 0], "r", linewidth=1.5)
plt.show()

pxiryf3j

pxiryf3j3#

如果您的目标是突出显示一个区域,那么使用蒙版反转的填充方法如何?我的方法是创建一个要填充的颜色数组,并在色彩Map表中指定要填充的颜色。然后您可以隐藏颜色栏。

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

images = []
vmin = 0
vmax = 80
cmap = "viridis"
size = 40
matrix = np.random.randint(vmin, vmax, size=(size,size))
np.random.seed(7)
mask = []
for _ in range(size):
    prefix_length = np.random.randint(size)
    mask.append([False]*prefix_length + [True]*(size-prefix_length))
mask = np.array(mask)
sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap="viridis", mask=mask)
colors = ['ivory']*size*size
sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap=colors, mask=~mask, cbar=False)

plt.savefig("temp.png")
plt.show()

相关问题