【问题标题】:Seaborn heatmap auto sizing the cellsSeaborn 热图自动调整单元格大小
【发布时间】:2020-11-16 22:47:00
【问题描述】:

我正在尝试构建一个热图,如果有很多分类变量,它会很好用,但当有两到三个数据点时看起来不太好,如第二张图片所示。我正在寻找一种根据数据点自动调整的方法。

这里是函数

def bivariate(col1,col2,Title,cbar_size):
    temp2=modifiedloan.groupby([col1,col2,'loan_status']).id.agg('count').to_frame('count').reset_index()
    temp3=temp2.pivot_table(index=(col1,col2), columns='loan_status', values='count').fillna(0)
    temp3['default%']=(temp3[0]/(temp3[0]+temp3[1]))
    temp3=temp3.reset_index()
    
    temp4=temp3.pivot_table(index=col1, columns=col2, values='default%').fillna(0)
    temp5=temp3.pivot_table(index=col1, columns=col2, values=[0]).fillna(0)
    f, (ax1) = plt.subplots(nrows=1, ncols=1, figsize=(12,6))

    cmap = sns.cm.rocket_r
    sns.heatmap(temp4,linewidths=1, ax=ax1,annot=False, fmt='g',cmap=cmap,cbar=True,cbar_kws={"shrink": cbar_size})

    sns.heatmap(temp5, annot=True, annot_kws={'va':'top'}, fmt="", cbar=False,ax=ax1)
    sns.heatmap(temp4, annot=True,  fmt=".1%",annot_kws={'va':'bottom'}, cbar=False,cmap=cmap)

    plt.ylim(b, t) # update the ylim(bottom, top) values    
    ax1.set_title(Title)


    plt.tight_layout()

【问题讨论】:

    标签: python seaborn heatmap


    【解决方案1】:

    我意识到问题出在安装的 seaborn 版本上。相同的功能在我同事的一台机器上运行良好。

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      相关资源
      最近更新 更多