数据可视化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()

你可能感兴趣的:(数据可视化)