سؤال

I used the tutorial from the matplotlib cookbook to create a customized color scale. The For some reason the color interpolation fails on the step from 0.8 to 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.

On a sidenote, does anyone know what the name argument is used for in the LinearSegmentedColormap? It is not mentioned in the documentation.

I am using matplotlib version 1.2.1 and 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

هل كانت مفيدة؟

المحلول

You have a typo in your last entry for green: 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)

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top