我有一个数据框df
:
df = pd.DataFrame(columns=["App","Feature1", "Feature2","Feature3", "Feature4","Feature5", "Feature6","Feature7","Feature8"], data=[['SHA', 0, 0, 1, 1, 1, 0, 1, 0], ['LHA', 1, 0, 1, 1, 0, 1, 1, 0], ['DRA', 0, 0, 0, 0, 0, 0, 1, 0], ['FRA', 1, 0, 1, 1, 1, 0, 1, 1], ['BRU', 0, 0, 1, 0, 1, 0, 0, 0], ['PAR', 0, 1, 1, 1, 1, 0, 1, 0], ['AER', 0, 0, 1, 1, 0, 1, 1, 0], ['SHE', 0, 0, 0, 1, 0, 0, 1, 0]])
# display(df)
App Feature1 Feature2 Feature3 Feature4 Feature5 Feature6 Feature7 Feature8
0 SHA 0 0 1 1 1 0 1 0
1 LHA 1 0 1 1 0 1 1 0
2 DRA 0 0 0 0 0 0 1 0
3 FRA 1 0 1 1 1 0 1 1
4 BRU 0 0 1 0 1 0 0 0
5 PAR 0 1 1 1 1 0 1 0
6 AER 0 0 1 1 0 1 1 0
7 SHE 0 0 0 1 0 0 1 0
我想创建一个堆积条形图,以便每个堆栈对应App
而 Y 轴将包含计数1
值和 X 轴将是Feature
.
它应该与此条形图类似,唯一的区别是现在我想查看堆栈条形图和带有颜色的图例:
df_c = df.iloc[:, 1:].eq(1).sum().rename_axis('Feature').reset_index(name='Count')
df_c = df_c.sort_values('Count')
plt.figure(figsize=(12,8))
ax = sns.barplot(x="Feature", y='Count', data=df_c, palette=sns.color_palette("GnBu", 10))
plt.xticks(rotation='vertical')
ax.grid(b=True, which='major', color='#d3d3d3', linewidth=1.0)
ax.grid(b=True, which='minor', color='#d3d3d3', linewidth=0.5)
plt.show()