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)
然后做一些事情,比如将低于某个值的所有值设置为零,以测试您可能拥有的解决方案
黑色通常对应于RGBA数组中的[0,0,0,1]。所以只需在事后将所有遮罩位置设置为这些值。在
相关问题 更多 >
编程相关推荐