2013-09-26 151 views
0

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

在旁注上,有没有人知道LinearSegmentedColormapname参数的用法是什么?它在文档中没有提及。

我使用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

回答

2

您的最后一个项目一个错字绿色: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

+0

哦,伙计,谢谢......我想我会继续工作之前喝一杯咖啡-.- – Faultier