【问题标题】:reduce space between dendrogram and color row in Seaborn Clustermap减少 Seaborn Clustermap 中树状图和颜色行之间的空间
【发布时间】:2021-07-06 11:09:50
【问题描述】:

大家好。我有两个问题。首先,有谁知道如何减少树状图和颜色行之间的距离。然后是如何减少颜色行和实际热图之间的距离。 其次,树状图的底部真的被压扁并且难以阅读。有谁知道如何更改树状图中的行长?

这是我用来创建热图的代码

def create_heatmap(df, method, figsize, row_colors=None):
    '''df should be in the form of a cosine similarity matrix'''
    # fig, (ax1,ax2) = plt.subplots(2)

    im = sns.clustermap(df,
                        method=method,
                        metric='cosine',
                        yticklabels=True, 
                        xticklabels=True, 
                        figsize=figsize, 
                        col_colors=row_colors,
                        cbar_pos = (.829,.832,.058,.019), 
                        cbar_kws={"orientation": "horizontal"})
#                         dendrogram_ratio= 0.15)
    sns.set(font_scale=1.18)    
    # plt.tight_layout()
    return im 


im =create_heatmap(cosine, method, figsize, row_colors)
im.ax_row_dendrogram.set_visible(False)
im.cax.yaxis.set_ticks_position("right")
im.cax.xaxis.set_ticks_position("top")

ax = im.ax_heatmap
ax.set_ylabel("")
plt.show()

【问题讨论】:

    标签: python pandas matplotlib seaborn data-visualization


    【解决方案1】:

    这是一个使用 iris 数据集创建类似内容的测试。要获得更大的树状图区域,您可以尝试使用dendrogram_ratio。下面的代码使用(.1, .8)。在当前版本(Seaborn 0.11.1)中,颜色行上下的距离似乎为零。

    import seaborn as sns;
    import matplotlib.pyplot as plt
    
    sns.set_theme(color_codes=True)
    iris = sns.load_dataset("iris")
    
    species = iris.pop("species")
    petal_length = iris.pop("petal_length")
    petal_width = iris.pop("petal_width")
    norm = plt.Normalize(petal_width.min(), petal_width.max())
    cmaps = {'setosa': 'Reds', 'versicolor': 'Greens', 'virginica': 'Blues'}
    row_colors = [plt.get_cmap(cmaps[sp])(norm(petal_w)) for sp, petal_w in zip(species, petal_width)]
    g = sns.clustermap(iris.T,
                       figsize=(7, 5),
                       row_cluster=False,
                       dendrogram_ratio=(.1, .8),
                       col_colors=row_colors,
                       cbar_pos=(.829, .832, .158, .019),
                       cbar_kws={"orientation": "horizontal"})
    plt.show()
    

    【讨论】:

    • 如果这回答了你的问题,你可以考虑投票和/或accepting答案。
    猜你喜欢
    • 2019-03-25
    • 2022-07-22
    • 2019-03-22
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多