【问题标题】:Python heatmap: Change colour map and make asymmetricalPython热图:更改颜色图并使不对称
【发布时间】:2018-09-06 22:00:12
【问题描述】:

我想为这些数据建立一个热图:

curation1       curation2       overlap
1      2      0
1      3      1098
1      4      11
1      5      137
1      6      105
1      7      338
2      3      351
2      4      0
2      5      1
2      6      0
2      7      0
3      4      132
3      5      215
3      6      91
3      7      191
4      5      6
4      6      10
4      7      19
5      6      37
5      7      95
6      7     146

我用这段代码制作了一张热图:

import sys
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.ticker as ticker
import matplotlib.cm as cm
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import colors

data_raw = pd.read_csv(sys.argv[1],sep = '\t')
data_raw["curation1"] = pd.Categorical(data_raw["curation1"], data_raw.curation1.unique())
data_raw["curation2"] = pd.Categorical(data_raw["curation2"], data_raw.curation2.unique())
data_matrix = data_raw.pivot("curation1", "curation2", "overlap")

fig = plt.figure()
fig, ax = plt.subplots(1,1, figsize=(12,12))
heatplot = ax.imshow(data_matrix,cmap = 'BuPu')
#ax.set_xticklabels(data_matrix.columns)
#ax.set_yticklabels(data_matrix.index)
tick_spacing = 1
#ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
#ax.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax.set_title("Overlap")
fig.savefig('output.pdf')

输出如下所示:

我有三个问题:

  1. 您可以看到配色方案有点“偏离”,因为大多数数据的颜色都很浅,并且有一个随机的紫色框表示“0”。理想情况下,我希望这张热图是不同深浅的绿色,最深的绿色是最高的数字,最轻(但仍然清晰可见)的绿色是最低的数字。我试图玩弄'cmap'参数,例如如python教程here中所述将其更改为“冬季”;但我做错了什么。有人可以告诉我具体在哪里可以更改吗?

  2. 彩条:我想加彩条,但我想我需要先理清问题1。

  3. 不对称:如您所见,此图是不对称的。是否可以绘制热图的一半(例如,去掉不必要的线条,并可能将轴标签移动到图的右侧?;如果不是,这没什么大不了的,因为我可以重新夹具它在PowerPoint中)。

【问题讨论】:

    标签: python pandas matplotlib heatmap


    【解决方案1】:

    这将解决你的前两个问题 -

    fig = plt.figure()
    fig, ax = plt.subplots(1,1, figsize=(12,12))
    heatplot = ax.imshow(data_matrix,cmap = 'Greens')
    
    cbar = fig.colorbar(heatplot, ticks=[data_raw.overlap.min(), data_raw.overlap.max()])
    tick_spacing = 1
    ax.set_title("Overlap")
    

    【讨论】:

    • 这太棒了,谢谢,因为我可以在 powerpoint 中完成最后一部分。我对自己的 cmap 参数尝试如此接近感到恼火!谢谢。
    【解决方案2】:

    我会改用seaborn heatmap 函数。颜色图Greens 应该可以解决您想要的配色方案。如果您愿意,可以查看matplotlib docs 中的其他选项。

    只需 hihglight 和 ctrl + c 问题中的数据集,然后运行下面的 sn-p:

    # Imports
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    
    data_raw = pd.read_clipboard(sep='\\s+')
    data_matrix = data_raw.pivot("curation1", "curation2", "overlap")
    data_matrix = data_matrix.fillna(0)
    
    # A heatmap function that builds on the seaborn heatmap function
    def HeatMap_function(df, title, transpose = True, colors = 'Greens', dropDuplicates = True):
    
        if transpose:
            df = df.T
        
        if dropDuplicates:    
            mask = np.zeros_like(df, dtype=np.bool)
            mask = np.invert(mask)
            mask[np.triu_indices_from(mask)] = False
    
        # Set background color / chart style
        sns.set_style(style = 'white')
    
        # Set up  matplotlib figure
        f, ax = plt.subplots(figsize=(11, 9))
        ax.set_title(title)
      
        # Add diverging colormap from red to blue
        # cmap = sns.diverging_palette(250, 10, as_cmap=True)
        cmap=plt.get_cmap(colors)
        
        # Draw correlation plot with or without duplicates
        if dropDuplicates:
            sns.heatmap(df, mask=mask, cmap=cmap, 
                    square=True,
                    linewidth=.5, cbar_kws={"shrink": .5}, ax=ax)
        else:
            sns.heatmap(df, cmap=cmap, 
                    square=True,
                    linewidth=.5, cbar_kws={"shrink": .5}, ax=ax)
    
        ax.xaxis.set_ticks_position('top')
        ax.yaxis.set_ticks_position('right')
    
    # A testrun
    HeatMap_function(df = data_matrix, title = 'Overlap', transpose = False,
                     colors = 'Greens', dropDuplicates = True)
    

    你会得到这个:

    现在您还可以使用transposecolorsdropDuplicates 的不同组合来更改绘图布局。

    【讨论】:

    • 如果这是您认为可以使用的东西,我将在几行中进行编辑,以便在我找到时间时将其存储在活动的 powerpoint 演示文稿中。
    【解决方案3】:

    从选择合适的颜色图开始(看看here),Greens 可能会很好。请注意,可以通过在名称中添加 '_r' 来反转颜色图。

    由于您的值差异很大,我将使用对数色标。 你可以通过包含color.LogNorm(来自import matplotlib.colors as colors)来做到这一点

    为了解决您的第三个问题,我会将轴移动到右上角并删除底部和左侧线。

    # Plot heatmap
    f, ax = plt.subplots()
    lognorm = colors.LogNorm(vmin = data.min(), vmax = data.max())
    heatplot  = ax.imshow(data, vmin = 1, norm = lognorm, cmap = 'Greens')
    
    # Move axis to top-right and remove bottom and left line
    ax.xaxis.set_ticks_position('top')
    ax.yaxis.set_ticks_position('right')
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)
    
    # Use instead of ax.set_title to move title a bit higher up
    f.suptitle('Overlap') 
    
    # Add colorbar
    cb = f.colorbar(heatplot)
    
    f.show()
    

    【讨论】:

      猜你喜欢
      • 2019-03-08
      • 2016-07-20
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多