Python 如何为 matplotlib 颜色图中的特定值重新定义颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16400241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redefine a color for a specific value in a matplotlib colormap
提问by otmezger
I want to use the colormap rainbowin 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.
我想rainbow使用imshow. 它工作得很好,但我想重新定义 value 的颜色0。我想用白色而不是紫罗兰色。
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 withouthaving to edit the colormap manually?
知道如何做到这一点而不必手动编辑颜色图吗?
采纳答案by tacaswell
You can also use set_underwhich I think makes more semantic sense than using set_bad
你也可以使用set_under我认为比使用更语义的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)
You can tweak the colorbar to also show the 'under' (and the symmetric 'over') color using the kwarg extend, see exampleand docs.
您可以使用 kwarg 调整颜色条以显示“under”(和对称的“over”)颜色extend,请参阅示例和文档。
For an answer to a duplicate with more complete examples see How to create matplotlib colormap that treats one value specially?
有关更完整示例的副本的答案,请参阅如何创建特别处理一个值的 matplotlib 颜色图?

