【问题标题】:How to create a heat map in python that ranges from green to red?如何在 python 中创建从绿色到红色的热图?
【发布时间】:2016-11-09 20:04:19
【问题描述】:

我正在尝试绘制 -3 到 3 范围内的对数比率,并希望负比率为绿色,正比率为红色,对数比率为 0(中心)为白色。 matplotlib 中预先存在的配色方案都没有提供此选项,而且我无法弄清楚如何手动输出漂亮的渐变。

【问题讨论】:

标签: python matplotlib heatmap


【解决方案1】:

下面使用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()

它产生以下

参见例如http://matplotlib.org/examples/pylab_examples/custom_cmap.html 更多用途和其他示例。

【讨论】:

    【解决方案2】:

    您可以使用LinearSegmentedColormap 创建自己的。我喜欢将红色和绿色通道的上限和下限设置为小于 1.0,这样颜色就不会太亮(这里我使用了 0.8)。调整它以适合您的口味。

    有关详细信息,请参阅 matplotlib 网站上的 custom_cmap example

    这是一个工作示例:

    import matplotlib.pyplot as plt
    import matplotlib.colors as colors
    import numpy as np
    
    # This dictionary defines the colormap
    cdict = {'red':  ((0.0, 0.0, 0.0),   # no red at 0
                      (0.5, 1.0, 1.0),   # all channels set to 1.0 at 0.5 to create white
                      (1.0, 0.8, 0.8)),  # set to 0.8 so its not too bright at 1
    
            'green': ((0.0, 0.8, 0.8),   # set to 0.8 so its not too bright at 0
                      (0.5, 1.0, 1.0),   # all channels set to 1.0 at 0.5 to create white
                      (1.0, 0.0, 0.0)),  # no green at 1
    
            'blue':  ((0.0, 0.0, 0.0),   # no blue at 0
                      (0.5, 1.0, 1.0),   # all channels set to 1.0 at 0.5 to create white
                      (1.0, 0.0, 0.0))   # no blue at 1
           }
    
    # Create the colormap using the dictionary
    GnRd = colors.LinearSegmentedColormap('GnRd', cdict)
    
    # Make a figure and axes
    fig,ax = plt.subplots(1)
    
    # Some fake data in the range -3 to 3
    dummydata = np.random.rand(5,5)*6.-3.
    
    # Plot the fake data
    p=ax.pcolormesh(dummydata,cmap=GnRd,vmin=-3,vmax=3)
    
    # Make a colorbar
    fig.colorbar(p,ax=ax)
    
    plt.show()
    

    【讨论】:

    • 我要补充一点,使用绿红颜色地图不是一个好主意,因为色盲的人将无法解释你的情节!
    • 以上评论是很好的建议,引用matplotplib’s colormaps documentation“最常见的色觉缺陷形式涉及区分红色和绿色。因此,避免使用红色和绿色的颜色图将避免许多一般问题。”
    【解决方案3】:

    使用matplotlib.colors.LinearSegmentedColormapfrom_list 方法似乎比这里的其他一些答案更直观。

    from  matplotlib.colors import LinearSegmentedColormap
    cmap=LinearSegmentedColormap.from_list('rg',["r", "w", "g"], N=256) 
    

    或者更复杂的调优:

    from  matplotlib.colors import LinearSegmentedColormap
    c = ["darkred","red","lightcoral","white", "palegreen","green","darkgreen"]
    v = [0,.15,.4,.5,0.6,.9,1.]
    l = list(zip(v,c))
    cmap=LinearSegmentedColormap.from_list('rg',l, N=256)
    

    【讨论】:

    • 感觉这应该是公认的答案!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 2023-01-13
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多