matplotlib自定义colorbar非预期的离散颜色

时间:2013-09-26 07:29:40

标签: python matplotlib color-mapping

我使用了the matplotlib cookbook中的教程来创建自定义色阶。由于某种原因,颜色插值在从0.8到1.0的步骤中失败。我不确定在这一步中我做错了什么,因为只有当相应元组的第二和第三个数字不同时才会出现离散的颜色步骤。我打算在最后一步从RGB 0/130/195到102/179/218。

在旁注中,是否有人知道nameLinearSegmentedColormap参数的用途是什么?文档中没有提到它。

我正在使用matplotlib版本1.2.1和Python 2.7.5

import pylab as P
import numpy as N
cdict = {'red':  ((0.0, 51.0/255, 51.0/255),
                   (0.2, 180.0/255, 180.0/255),
                   (0.4, 175.0/255, 175.0/255),
                   (0.6, 206.0/255, 206.0/255),
                   (0.8, 0.0/255, 0.0/255),
                   (1.0, 102.0/255, 102.0/255)),
    'green':((0.0, 51.0/255, 51.0/255),
                   (0.2, 180.0/255, 180.0/255),
                   (0.4, 200.0/255, 200.0/255),
                   (0.6, 211.0/255, 211.0/255),
                   (0.8, 130.0/255, 130.0/255),
                   (1.0, 217.0/25, 217.0/255)),
    'blue': ((0.0, 51.0/255, 51.0/255),
                   (0.2, 180.0/255, 180.0/255),
                   (0.4, 7.0/255, 7.0/255),
                   (0.6, 106.0/255, 106.0/255),
                   (0.8, 195.0/255, 195.0/255),
                   (1.0, 237.0/255, 237.0/255))
        }
res_map = P.matplotlib.colors.LinearSegmentedColormap('my_cmap',cdict,256)
P.figure()    
P.pcolor(N.reshape(N.linspace(0,100,100*100), (100,100)),cmap=res_map)
P.colorbar()
P.show()

the undesired output

1 个答案:

答案 0 :(得分:3)

您在绿色的最后一个条目中输入了拼写错误:217.0/25

这有效:

cdict = {'red':  ((0.0, 51.0/255, 51.0/255),
                   (0.2, 180.0/255, 180.0/255),
                   (0.4, 175.0/255, 175.0/255),
                   (0.6, 206.0/255, 206.0/255),
                   (0.8, 0.0/255, 0.0/255),
                   (1.0, 102.0/255, 102.0/255)),

        'green':((0.0, 51.0/255, 51.0/255),
                   (0.2, 180.0/255, 180.0/255),
                   (0.4, 200.0/255, 200.0/255),
                   (0.6, 211.0/255, 211.0/255),
                   (0.8, 130.0/255, 130.0/255),
                   (1.0, 217.0/255, 217.0/255)),

        'blue': ((0.0, 51.0/255, 51.0/255),
                   (0.2, 180.0/255, 180.0/255),
                   (0.4, 7.0/255, 7.0/255),
                   (0.6, 106.0/255, 106.0/255),
                   (0.8, 195.0/255, 195.0/255),
                   (1.0, 237.0/255, 237.0/255))
        }

res_map = plt.matplotlib.colors.LinearSegmentedColormap('my_cmap',cdict,256)

enter image description here