如何在matplotlib颜色图中为特定值重新定义颜色 [英] How to redefine a color for a specific value in a matplotlib colormap

查看:190
本文介绍了如何在matplotlib颜色图中为特定值重新定义颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用imshow的图像中使用颜色映射rainbow.它工作正常,但是我想重新定义值0的颜色.我要使用白色代替紫罗兰色.

I want to use the colormap rainbow in an image using imshow. It works perfectly, but I want to redefine the color for the value 0. Instead of violete, I want to use white.

我只想对零值执行此操作,所有其他值可以保留在颜色图中的默认值.

I want to do this only for the value zero, all other values can stay with the default values in the colormap.

有什么想法可以无需手动编辑颜色图吗?

Any idea how to do this without having to edit the colormap manually?

推荐答案

您还可以使用set_under,我认为它比使用set_bad

You can also use set_under which I think makes more semantic sense than using set_bad

my_cmap = matplotlib.cm.get_cmap('rainbow')
my_cmap.set_under('w')
imshow(np.arange(25).reshape(5, 5),
       interpolation='none',
       cmap=my_cmap,
       vmin=.001)

您可以使用kwarg extend调整颜色栏以显示下方"(和对称的上方")颜色,请参见文档.

You can tweak the colorbar to also show the 'under' (and the symmetric 'over') color using the kwarg extend, see example and docs.

有关重复示例的更完整示例的答案,请参见

For an answer to a duplicate with more complete examples see How to create matplotlib colormap that treats one value specially?

这篇关于如何在matplotlib颜色图中为特定值重新定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆