如何从ScalarMappable obj获取一个屏蔽的rgba数组

2024-05-29 03:11:54 发布

您现在位置:Python中文网/ 问答频道 /正文

to_rgba状态的文档:

Note: this method assumes the input is well-behaved; it does not check for anomalies such as x being a masked rgba array, or being an integer type other than uint8, or being a floating point rgba array with values outside the 0-1 range.

但这正是我需要的:一个屏蔽的rgba阵列。以下是改编自this post并给出了正确的值,除了nan(我在第一行中设为零)的值应该是spc_map_color中的黑色rgba值

spc_map[np.isnan(spc_map)] = 0
gradient_range = matplotlib.colors.Normalize(-1.0, 1.0)
cmap = matplotlib.cm.ScalarMappable(
  gradient_range, self.cm_type)
spc_map_color = cmap.to_rgba(spc_map, bytes=True)

如您所见,使用的colorMap是可变的,但是对于掩码之外的值(即nan)返回的rgba值应该是不变的,即始终为黑色。在

关于如何对linearsegmentedcolourmap对象执行此操作的答案存在here和{a3}。但是,如果您想用imshow输出图像,就可以这样做了。我不想那样做。我想要一个数组中的rgba值。在

在转换为rgba之前,我尝试过cmap.set_bad(color='black')作为我的ScalarMappable,但是ScalarMappable没有set_bad函数。在

我错过了一些明显的东西?在

你可以用 spc_map = np.random.randn(256,256)然后做一些事情,比如将低于某个值的所有值设置为零,以测试您可能拥有的解决方案


Tags: orthetomaptyperangenanthis
1条回答
网友
1楼 · 发布于 2024-05-29 03:11:54

黑色通常对应于RGBA数组中的[0,0,0,1]。所以只需在事后将所有遮罩位置设置为这些值。在

# get mask of masked array
mask = spc_map.mask 

# RGBA array should have one more dimension than spc_map with length 4 
spc_map_color[mask] = np.array([0, 0, 0, 1])

相关问题 更多 >

    热门问题