gpt4 book ai didi

python - 如何使用 seaborn 为我的 DataFrame 创建堆叠条形图

转载 作者:太空狗 更新时间:2023-10-29 17:11:32 24 4
gpt4 key购买 nike

<分区>

我有一个 DataFrame 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 轴将是功能

它应该类似于这个条形图,唯一的区别是现在我想看到堆栈条和带有颜色的图例:

df_c = df.iloc[:, 1:].eq(1).sum().rename_axis('Feature').reset_index(name='Cou‌nt')
df_c = df_c.sort_values('Cou‌nt')
plt.figure(figsize=(12,8))
ax = sns.barplot(x="Feature", y='Cou‌nt', 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()

enter image description here

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com