- Signature:
sns.distplot(a, bins=None, hist=True, kde=True)
该图的成绩分段使用系统默认的设置。结果整体成绩是否为“左偏”?确实是“左偏”。
总体成绩的hist & kde:了解总体分布情况# set bins
fig,(ax1,ax2)= plt.subplots(1,2,sharex=True, figsize=(7,5))
plt.subplot(1,2,1)
ax1 = sns.distplot(a=df['glkj'], bins=[10, 20, 30, 40, 50, 60, 70, 80, 90,100],
norm_hist= False,hist=True, kde=False,label='管理会计成绩')
ax1.set(title='19财管管理会计成绩',xlabel='管理会计成绩',ylabel='Count')
ax1.legend(loc='best')
#plt.tight_layout(rect=(1, 1, 1, 1)) #设置默认的间距
plt.subplot(1,2,2)
ax2 = sns.distplot(a=df['glkj'], bins=[10, 20, 30, 40, 50, 60, 70, 80, 90,100],
norm_hist= True,hist=True, kde=True,label='管理会计成绩KDE',color='green')
ax2.set(title='19财管管理会计成绩KED',xlabel='管理会计成绩',ylabel='P')
ax2.legend(loc='best')
plt.subplots_adjust(wspace=0.3)
plt.show()
bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 101]
labels = ['0-10','10-20','20-30','30-40','40-50','50-60','60-70','70-80','80-90','90 ']
- x:需要切分的数据
- bins:切分区域
- right : 是否包含右端点默认True,包含
- labels:对应标签,用标记来代替返回的bins,若不在该序列中,则返回NaN
- retbins:是否返回间距bins
- precision:精度
- include_lowest:是否包含左端点,默认False,不包含
- right : 是否包含右端点默认True,包含。该例为不包括False。[a,b)
df['glkj_bins'] = pd.cut(df['glkj'], bins=bins, labels=labels, include_lowest=True, right=False)
class_count = df.groupby(by= 'class')['glkj_bins'].value_counts()
pd_class_count= pd.DataFrame(class_count)
pd_unstack = pd_class_count.unstack(fill_value=0)
for i in range(6):
fig,(ax1,ax2)= plt.subplots(1,2,sharex=True,figsize=(8,6))
plt.subplot(1,2,1)
ax1 = sns.barplot(count_bins,pd_unstack.values[i],label=pd_unstack.index[i])
ax1.legend(loc='best')
ax1.set(xlabel= '管理会计分段成绩',ylabel= 'Count',title = '管理会计分班级成绩图')
list_n = pd_unstack.values[i]
for j, txt in enumerate(list_n):
ax1.annotate(txt, (j, list_n[j] 0.6),horizontalalignment='center',verticalalignment='center')
plt.subplot(1,2,2)
ax2 = sns.distplot(a=df.loc[df['class']== pd_unstack.index[i]]['glkj'],bins=[10, 20, 30, 40, 50, 60, 70, 80, 90,100],norm_hist= True,hist=True, kde=True,label= pd_unstack.index[i],color='green')
ax2.set(title='管理会计分班级成绩kde',xlabel='管理会计成绩',ylabel='P')
ax2.legend(loc='best')
plt.show()