简体   繁体   中英

Seaborn heatmap auto sizing the cells

I am trying to build a heat map which is working great if there are lot of categorical variables but not looking great when there are two to three data points as shown in the second image. I am looking for a way to auto adjust based on the data points.

在此处输入图像描述

在此处输入图像描述

here is the function

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()

I realized the problem is with the version of seaborn installed. The same functions worked well on one of my colleagues machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM