2013-09-26 152 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