1. 程式人生 > >資料視覺化seaborn(countplot定製)

資料視覺化seaborn(countplot定製)

sns.countplot能顯示該列資料值的統計分佈,要再顯示百分比,程式如下

def add_freq():
    ncount=len(train)
    ax2=ax.twinx()
    ax2.yaxis.tick_left()
    ax.yaxis.tick_right()
    ax.yaxis.set_label_position('right')
    ax2.yaxis.set_label_position('left')
    ax2.set_ylabel('Frequency [%]')
    for p in ax.patches:
        x=p.get_bbox().get_points()[:,0]
        y=p.get_bbox().get_points()[1,1]
        ax.annotate('{:.1f}%'.format(100.*y/ncount),(x.mean(),y),ha='center',va='bottom')
    ax2.set_ylim(0,100)
    ax2.grid(None)

ax=sns.countplot(x=train['SeriousDlqin2yrs'],palette='Set3')
#sns.set(font_scale=1.5)
#ax.set_ylim(top=150000)
#ax.set_xlabel(' ')
#ax.set_ylabel(' ')
#fig=plt.gcf()
#fig.set_size_inches(10,5)
#ax.set_ylim(top=160000)
add_freq()
plt.show()