Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用多个类别打印遮罩,并忽略背景(matplotlib)_Python 3.x_Matplotlib_Imshow - Fatal编程技术网

Python 3.x 使用多个类别打印遮罩,并忽略背景(matplotlib)

Python 3.x 使用多个类别打印遮罩,并忽略背景(matplotlib),python-3.x,matplotlib,imshow,Python 3.x,Matplotlib,Imshow,我有两个数组:一个图像和一个掩码。遮罩有不同的类别:0到4。我想在图像上绘制一个覆盖图,但不显示“0”类别(只是不显示它,或者让它完全透明) 这是我的代码: # Plot fig, ax = plt.subplots(2, 4, dpi=300, figsize=(20, 10)) cmap_mask = ListedColormap(['black', 'red', 'green', 'yellow']) for index, channel in enumerate(channels):

我有两个数组:一个图像和一个掩码。遮罩有不同的类别:0到4。我想在图像上绘制一个覆盖图,但不显示“0”类别(只是不显示它,或者让它完全透明)

这是我的代码:

# Plot
fig, ax = plt.subplots(2, 4, dpi=300, figsize=(20, 10))
cmap_mask = ListedColormap(['black', 'red', 'green', 'yellow'])
for index, channel in enumerate(channels):
    # Top row without masks
    ax[0, index].imshow(image[index, slice, :, :], cmap='gray')
    ax[0, index].set_title(channel, fontweight='bold')
    ax[0, index].get_xaxis().set_visible(False)
    ax[0, index].get_yaxis().set_visible(False)
    # Bottom row with masks
    ax[1, index].imshow(image[index, slice, :, :], cmap='gray')
    ax[1, index].imshow(mask[slice, :, :], alpha=.7, cmap=cmap_mask)
    ax[1, index].get_xaxis().set_visible(False)
    ax[1, index].get_yaxis().set_visible(False)

我已经在这上面花了太长时间了。情节看起来不错,但我正在把背景调暗(因为是黑色的,但这是迄今为止我得到的最好的情节)。我试着在
np.ma.masked_那里使用
,但我没能让它工作。

你可能想试试
cmap_mask=ListedColormap(['none','red','green','yellow'])
我真不敢相信这有那么容易。。。