How to create a heat map in python that ranges from green to red?

前端 未结 3 616
花落未央
花落未央 2020-12-10 05:25

I\'m trying to plot log ratios from the range -3 to 3 and want negative ratios to be green and positive to be red, with a log ratio of 0 (center) to be white in color. None

3条回答
  •  青春惊慌失措
    2020-12-10 05:40

    How about the following that uses LinearSegmentedColormap:

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.colors import LinearSegmentedColormap
    
    
    cmapGR = LinearSegmentedColormap(
        'GreenRed',
        {
            'red':  ((0.0, 0.0, 0.0),
                    (0.5, 1.0, 1.0),
                    (1.0, 1.0, 1.0)),
            'green':((0.0, 1.0, 1.0),
                    (0.5, 1.0, 1.0),
                    ( 1.0, 0.0, 0.0)),
            'blue': ((0.0, 0.0, 0.0),
                    (0.5, 1.0, 1.0),
                    (1.0, 0.0, 0.0))
        },)
    
    plt.imshow(np.array([np.arange(200) for i in range(200)]), cmap=cmapGR)
    plt.show()
    

    It produces the following

    See e.g. http://matplotlib.org/examples/pylab_examples/custom_cmap.html for more uses and other examples.

提交回复
热议问题