pcolor的自定义颜色映射
问题描述:
我遵循documentation来创建自定义颜色映射。地图我想是这样的:pcolor的自定义颜色映射
这是我想出了字典:
cdict = {'red': ((0.00, 0.0, 0.0),
(0.25, 1.0, 0.0),
(0.50, 0.0, 0.0),
(0.75, 1.0, 0.0),
(1.00, 1.0, 0.0)),
'green': ((0.00, 0.0, 0.0),
(0.25, 1.0, 1.0),
(0.50, 1.0, 1.0),
(0.75, 1.0, 1.0),
(1.00, 0.0, 0.0)),
'blue': ((0.00, 1.0, 1.0),
(0.25, 0.0, 0.0),
(1.00, 0.0, 0.0))
}
但它不给我我想要的结果。例如,值0.5使用红色渲染。
这里是代码的其余部分看起来像:
cmap = LinearSegmentedColormap('bgr', cdict)
plt.register_cmap(cmap=cmap)
plt.pcolor(dist, cmap='bgr')
plt.yticks(np.arange(0.5, len(dist.index), 1), dist.index)
plt.xticks(np.arange(0.1, len(dist.columns), 1), dist.columns, rotation=40)
for y in range(dist.shape[0]):
for x in range(dist.shape[1]):
plt.text(x + 0.5, y + 0.5, dist.iloc[y,x],
horizontalalignment='center',
verticalalignment='center', rotate=90
)
plt.show()
这里所呈现的热图的示例:
我缺少什么?
答
之所以说0.5的显示为红色在你的情节很可能只是因为你的vmin
和vmax
不是0.0和1.0。大多数matplotlib 2D绘图程序默认情况下将vmax设置为数组中的最大值,在您的情况下它看起来像是0.53。如果您希望0.5为绿色,请在致电pcolor
时设置vmin=0.0, vmax=1.0
。
你的颜色表字典几乎是正确的,但你拥有它现在有难以过渡到黄色/绿色在0.25和0.75点,你应该改变在“红色”这些线路从
(0.25, 1.0, 0.0),
(0.50, 0.0, 0.0),
(0.75, 1.0, 0.0),
到
(0.25, 1.0, 1.0),
(0.50, 0.0, 0.0),
(0.75, 1.0, 1.0),
得到你想要的colorscale。这是结果:
答
看来你的彩色字典是错误的。例如,用于开始色彩映射的第一个条目是:
'red': (0.00, 0.0, 0.0)
'green': (0.00, 0.0, 0.0)
'blue': (0.00, 1.0, 1.0)
它给出RGB = 001 =蓝色。另外,我不确定LinearSegmentedColormap
在某些时间间隔(如blue
中的索引0.5
)未定义时会如何运作。
这似乎给正确的结果:
import numpy as np
import matplotlib.pyplot as pl
from matplotlib.colors import LinearSegmentedColormap
pl.close('all')
cdict = {
'red': ((0.00, 1.0, 1.0),
(0.25, 1.0, 1.0),
(0.50, 0.0, 0.0),
(0.75, 1.0, 1.0),
(1.00, 0.0, 0.0)),
'green': ((0.00, 0.0, 0.0),
(0.25, 1.0, 1.0),
(0.50, 1.0, 1.0),
(0.75, 1.0, 1.0),
(1.00, 0.0, 0.0)),
'blue': ((0.00, 0.0, 0.0),
(0.25, 0.0, 0.0),
(0.50, 0.0, 0.0),
(0.75, 0.0, 0.0),
(1.00, 1.0, 1.0))
}
cm_rgb = LinearSegmentedColormap('bgr', cdict)
pl.figure()
pl.imshow(np.random.random((20,20)), interpolation='nearest', cmap=cm_rgb)
pl.colorbar()
为LinearSegmentedColormap
文档见the matplotlib docs。
哦,我看到我得到了彩色地图图像错误。不,这个词是对的,这只是图像必须是反映ti的一面镜子。我只是修正了这一点。问题是.5的值被映射为红色而不是绿色。你提供的文档链接清楚地表明r,g,b条目不需要对齐。在这个例子中,其中一个颜色分量比其他颜色分量有更多的间隔。 –