1、类别柱状图
用于展示类别特征在当前维度下的种类和占比
def barPlot(df,feature,figsize=(14, 6),save=False, filename=None):
feature_name = feature#.capitalize()
df_temp = df.copy()
df_temp[feature] = df_temp[feature].fillna('null')
#plt.figure(figsize=figsize)
#plt.suptitle(f'{feature_name} Distributions', fontsize=22)
# 柱形图:该特征下各类型占比
plt.subplot(111)
g = sns.countplot(x=feature, data=df_temp.sort_values(by=feature))
plt.xticks(rotation=60)
g.set_title(f"{feature_name}", fontsize=19)
g.set_xlabel(feature_name, ,fontsize=17)
g.set_ylabel("Count", fontsize=17)
for p in g.patches:
height = p.get_height()
g.text(p.get_x()+p.get_width()/2.,
height + 3,
'{:1.2f}%'.format(height/df.shape[0]*100),
ha="center", fontsize=14)
plt.xticks(rotation=60)
if save: # 保存图片
if filename:
plt.savefig(f'../pics/{filename}.png', bbox_inches='tight', dpi=300)
else:
plt.savefig(f'../pics/{feature_name}.png', bbox_inches='tight', dpi=300)
plt.show()
2、柱状&折现图
用于展示类别特征的数量分布,和各类别下命中target占比趋势。
import matplotlib.pyplot as plt
import matplotlib as mlp
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = [&