简体   繁体   English

如何将其绘制为堆积的条形图?

[英]How can i graph this as a stacked bar chart?

I have this code below, it produces my data frame exactly how i want it, but i can't seem to graph it via a grouped bar chart, 我在下面有这段代码,它按照我想要的方式生成我的数据框,但是我似乎无法通过分组的条形图对其进行绘图,

I'd like to have the department on the X axis and on the Y axis have completed with the remaining information on top 我想完成X轴和Y轴上的部门,其余信息放在顶部

在此处输入图片说明

import pandas as pd
import matplotlib

data = pd.read_excel('audit.xls', skiprows=2, index_col = 'Employee Department')
data.rename(columns = {'Curriculum Name':'Curriculum','Organization Employee Number':'Employee_Number', 'Employee Name': 'Employee','Employee Email':'Email', 'Employee Status':'Status', 'Date Assigned':'Assigned','Completion Date':'Completed'}, inplace=True)
data.drop(['Employee_Number', 'Employee','Assigned', 'Status', 'Manager Name', 'Manager Email', 'Completion Status','Unnamed: 1', 'Unnamed: 5', 'Unnamed: 6'], axis=1, inplace=True)

new_data = data.query('Curriculum ==["CARB Security Training","OIS Support Training","Legal EO Training"]')
new_data2 = new_data.groupby('Employee Department').count().eval('Remaining = Email - Completed', inplace=False)

new_data2

I assume i need to convert it to a pivot table somehow since that's how it is in excel 我认为我需要以某种方式将其转换为数据透视表,因为这就是excel中的方式

Have you tried something like this: new_data2[['Completed','Remaining']].plot.bar(stacked=True) 您是否尝试过这样的操作: new_data2[['Completed','Remaining']].plot.bar(stacked=True)

The following example works for me: 以下示例对我有用:

df = pd.DataFrame(np.arange(1,10).reshape(3,3), columns=['Email', 'Completed', 'Remaining'], index=['A', 'B', 'C'])
df[['Completed', 'Remaining']].plot.bar(stacked=True)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM