Python Matplotlib Colormap

前端 未结 3 1504
旧时难觅i
旧时难觅i 2020-12-18 10:34

I use the colormap \"jet\" to plot my graphics. But, I would like to have the lower values in white color and this colormap goes from blue to red colors. I also don\'t want

3条回答
  •  执笔经年
    2020-12-18 10:57

    Probably there should be an easiest solution, but the way I figured out is by creating your own matplotlib.colors.LinearSegmentedColormap, based on the "jet" one.

    (The lowest level of your colormap is defined in the first line of each tuple of red, green, and blue, so that's where you start editting. I add one extra tuple to have a clearly white spot in lower part. ...for each color, in the first element of the tuple you indicate the position in your colorbar (from 0 to 1), and in the second and third the color itself).

    from matplotlib.pyplot import *
    import matplotlib
    import numpy as np
    
    cdict = {'red': ((0., 1, 1),
                     (0.05, 1, 1),
                     (0.11, 0, 0),
                     (0.66, 1, 1),
                     (0.89, 1, 1),
                     (1, 0.5, 0.5)),
             'green': ((0., 1, 1),
                       (0.05, 1, 1),
                       (0.11, 0, 0),
                       (0.375, 1, 1),
                       (0.64, 1, 1),
                       (0.91, 0, 0),
                       (1, 0, 0)),
             'blue': ((0., 1, 1),
                      (0.05, 1, 1),
                      (0.11, 1, 1),
                      (0.34, 1, 1),
                      (0.65, 0, 0),
                      (1, 0, 0))}
    
    my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)
    
    pcolor(np.random.rand(10,10),cmap=my_cmap)
    colorbar()
    show()
    

    You'll get the following: jet colormap with white

提交回复
热议问题