有没有其他方法可以在不使用 matplotlib 的情况下找到百分比并绘制组条形图?

Posted

技术标签:

【中文标题】有没有其他方法可以在不使用 matplotlib 的情况下找到百分比并绘制组条形图?【英文标题】:Is there any other way to find percentage and plot a group bar-chart without using matplotlib? 【发布时间】:2020-11-20 13:22:07 【问题描述】:
emp_attrited = pd.DataFrame(df[df['Attrition'] == 'Yes'])
emp_not_attrited = pd.DataFrame(df[df['Attrition'] == 'No'])
print(emp_attrited.shape)
print(emp_not_attrited.shape)


att_dep = emp_attrited['Department'].value_counts()
percentage_att_dep = (att_dep/237)*100
print("Attrited")
print(percentage_att_dep)
not_att_dep = emp_not_attrited['Department'].value_counts()
percentage_not_att_dep = (not_att_dep/1233)*100
print("\nNot Attrited")
print(percentage_not_att_dep)

fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(221)
index = np.arange(att_dep.count())
bar_width = 0.15
rect1 = ax1.bar(index, percentage_att_dep, bar_width, color = 'black', label = 'Attrited')
rect2 = ax1.bar(index + bar_width, percentage_not_att_dep, bar_width, color = 'green', label = 'Not Attrited')
ax1.set_ylabel('Percenatage')
ax1.set_title('Comparison')
xTickMarks = att_dep.index.values.tolist()
ax1.set_xticks(index + bar_width)
xTickNames = ax1.set_xticklabels(xTickMarks)
plt.legend()
plt.tight_layout()
plt.show()
    第一个块表示如何根据 Attrition 将数据集分成 2 个 第二个块表示计算每个部门中减员和未减员的员工百分比。 第三个块是将给定的图表绘制为分组图表。

【问题讨论】:

【参考方案1】:

你可以这样做:

(df.groupby(['Department'])
   ['Attrited'].value_counts(normalize=True)
   .unstack('Attrited')
   .plot.bar()
)

【讨论】:

以上是关于有没有其他方法可以在不使用 matplotlib 的情况下找到百分比并绘制组条形图?的主要内容,如果未能解决你的问题,请参考以下文章

更改 matplotlib 子图的大小

在不手动计算子图数量的情况下创建 matplotlib 子图?

C++:有没有办法在不暴露其他私有成员的情况下限制对某些类的某些方法的访问?

如何在不更改 matplotlib 默认值的情况下使用 seaborn?

有没有一种方法可以在不使用 APi 网关架构的情况下使用 Jwt 保护微服务端点

仅在使用 matplotlib 的圆圈内绘制绘图 [重复]