堆叠颜色图 [英] stacking colormaps
问题描述
是否有一种简单的方法通过将两个现有的颜色图堆叠在一起来形成新的颜色图?
Is there a simple way to form a new colormap by stacking together two existing ones?
我要实现的目标是制作另一个颜色编码的散点图,其中颜色映射的变量从大的负值到大的正值不等,我想调低零附近的值- -基本上,我希望能够从普通颜色图(例如,cm.Blues_r
)中选择颜色映射变量的负值,并从其他颜色中(例如,cm.Oranges
)中选择颜色.该变量.
What I'm trying to achieve is to make yet another color-coded scatter plot, where the color-mapped variable varies from large negative to large positive values, and I'd like to tone down the values around zero --- basically, I'd like to be able to pick colors from a stock colormap (say, cm.Blues_r
) for negative values of the color-mapped variable, and from a different one (say, cm.Oranges
) for positive values of that variable.
推荐答案
未经测试,但是作为第一步,我将尝试制作一个colors.Colormap
的简单子类.
This isn't tested, but as a first pass I would try making a simple sub-class of colors.Colormap
.
class split_cmap(colors.Colormap):
def __init__(self, cmap_a, cmap_b, split=.5):
'''Makes a split color map cmap_a is the low range,
cmap_b is the high range
split is where to break the range
'''
self.cmap_a, self.cmap_b = cmap_a, cmap_b
self.split = split
def __call__(self, v):
if v < self.split:
return self.cmap_a(v)
# or you might want to use v / self.split
else:
return self.cmap_b(v)
# or you might want to use (v - self.split) / (1 - self.split)
def set_bad(self,*args, **kwargs):
self.cmap_a.set_bad(*args, **kwargs)
self.cmap_b.set_bad(*args, **kwargs)
def set_over(self, *args, **kwargs):
self.cmap_a.set_over(*args, **kwargs) # not really needed
self.cmap_b.set_over(*args, **kwargs)
def set_under(self, *args, **kwargs):
self.cmap_a.set_under(*args, **kwargs)
self.cmap_b.set_under(*args, **kwargs) # not really needed
def is_gray(self):
return False
您还将需要深入研究Normalize
类.彩色贴图只知道[0, 1]
,因此您必须确保将norm
映射到要进行转换的.5
.
You are going to need to dig into the Normalize
classes as well. The color maps only know about [0, 1]
, so you will have to make sure that your norm
maps to .5
where you want the change over to happen.
您可能会对此进行概括,以获取地图和拆分点的列表,并根据需要拥有尽可能多的彩色地图.这还需要各种方式的健全性检查.
You could probably generalize this to take a list of maps and split points and have as many color maps as you want. This also needs all manner of sanity checks.
如果您重新标准化输入,还可以通过将颜色图及其反向伙伴传递给现有颜色图,来制作 any 现有颜色图的定期版本.
If you re-normalize the input, you could also use this to make a periodic version of any existing color map by passing it the color map and it's reversed partner.
这篇关于堆叠颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!