简体   繁体   English

matplotlib自定义颜色条意外的离散颜色

[英]matplotlib custom colorbar unintended discrete colors

I used the tutorial from the matplotlib cookbook to create a customized color scale. 我使用了matplotlib食谱中的教程来创建自定义的色阶。 The For some reason the color interpolation fails on the step from 0.8 to 1.0. 由于某种原因,颜色插值在从0.8到1.0的步骤上失败。 I am not sure what I did wrong in this step, as discrete color steps should only occur if the second and third number of the respective tuple are different. 我不确定在此步骤中做错了什么,因为仅当相应元组的第二个和第三个数字不同时才发生离散的颜色步骤。 I intend to get from the RGB 0/130/195 to 102/179/218 in the last step. 我打算在最后一步将RGB 0/130/195转换为102/179/218。

On a sidenote, does anyone know what the name argument is used for in the LinearSegmentedColormap ? 在旁注中,是否有人知道LinearSegmentedColormapname参数是什么? It is not mentioned in the documentation. 文档中未提及。

I am using matplotlib version 1.2.1 and Python 2.7.5 我正在使用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()

不需要的输出

You have a typo in your last entry for green: 217.0/25 您上次输入绿色时有错字: 217.0/25

This works: 这有效:

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM