当我使用下面的代码并尝试运行它时。它完美地显示了所有内容,除了每个条形图中显示的 0.00%。
dt = pd.DataFrame({'witness':['No', 'No', 'No', 'No', 'No', 'No', 'No', 'Yes', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No', 'No'],
'category':['Sport', 'Sport', 'Sport', 'Sport', 'Sport', 'Sport', 'Sport', 'Sport', 'Utility', 'Sedan', 'Sport', 'Sport', 'Sport', 'Sport', 'Sedan', 'Sedan', 'Sport', 'Sport', 'Sport', 'Sedan', 'Sport', 'Sport', 'Sedan', 'Sport', 'Sedan'],
'make':['Honda', 'Honda', 'Honda', 'Toyota', 'Honda', 'Honda', 'Honda', 'Honda', 'Ford', 'Mazda', 'Honda', 'Ford', 'Ford', 'Ford', 'Ford', 'Chevrolet', 'Pontiac', 'Honda', 'Mazda', 'Chevrolet', 'Mazda', 'Pontiac', 'Mazda', 'Pontiac', 'Honda'],
'sex':['Female', 'Male', 'Male', 'Male', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Female', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male', 'Male'],
'fraud':[0, 0, 0, 0,1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0,1]})
fea = ['witness', 'category', 'make', 'sex']
fig, axes =plt.subplots(10,2,figsize=(10,40))
plt.subplots_adjust(wspace=0.9, hspace=0.9)
i=0
total = len(dt['witness'])
for fea_name in fea:
ax= axes[i//2, i%2]
i += 1
sns.countplot(x=fea_name, data=dt, ax=ax,hue='fraud' )
ax.set_title(f'Countplot of {fea_name}')
ax.tick_params(axis='x', rotation=45)
for i in range(len(fea), 10 * 2):
fig.delaxes(axes[i//2, i%2])
for ax in axes.flat:
for p in ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_height() / total)
x = p.get_x() + p.get_width()/3
y = p.get_y() + p.get_height()
ax.annotate(percentage, (x, y))
plt.tight_layout()
plt.show()
结果显示条形图很好,但是
主要问题是您要根据整个数据集中的记录总数计算百分比
total = len(dt['witness'])
。所以,你需要在for fea_name in fea:
中调用total
for fea_name in fea:
ax = axes[i//2, i%2]
i += 1
sns.countplot(x=fea_name, data=dt, ax=ax, hue='fraud')
ax.set_title(f'Countplot of {fea_name}')
ax.tick_params(axis='x', rotation=45)
total = len(dt[fea_name]) #
for p in ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_height() / total)
x = p.get_x() + p.get_width()/3
y = p.get_height()
ax.annotate(percentage, (x, y), ha='center')
for j in range(len(fea), 10 * 2):
fig.delaxes(axes[j//2, j%2])