我有一些excel数据,我正在尝试为其生成图形。
我编写了以下代码来生成图形以基于分组显示图形。我已经在“案例打开”图中使用了这种条件。
df.groupby(['Case Opened Quarter', 'Month Opened'], sort=False)[
'Case Number'].size().unstack().plot(kind='bar', stacked=True, legend=False)
“已结案”图显示了按季度划分的季度中按季度划分的案例。
我用于图形的代码
CaseDetails = {
'Case Number': [1029, 1056, 1207, 1436, 1655, 2054, 2314, 2786, 3013, 3435, 3711],
'Status': ['Closed', 'Closed', 'Assigned', 'Assigned', 'Closed', 'Closed', 'Assigned', 'Closed', 'Closed', 'Assigned', 'Closed'],
'Date/Time Opened': ['2/2/2017 2:13 AM', '3/01/2017 12:09 AM', '3/23/2017 2:09 PM', '7/12/2017 2:41 PM', '9/24/2017 11:24 PM', '2/10/2018 1:41 AM', '3/4/2018 10:45 AM', '4/20/2018 10:21 AM', '4/30/2018 2:10 AM', '6/6/2018 4:40 PM', '9/12/2018 5:10 PM'],
'Date/Time Closed': ['4/5/2017 7:52 AM', '4/23/2018 6:10 AM', '', '', '10/1/2017 11:35 PM', '6/19/2018 5:43 AM', '', '2/10/2019 7:50 PM', '8/12/2018 12:10 PM', '', '9/20/2018 10:21 PM'],
'VMware Owner': ['Person 1', 'Person 2', 'Person 3', 'Person 4', 'Person 1', 'Person 5', 'Person 6', 'Person 3', 'Person 4', 'Person 1', 'Person 7'],
'Month Opened': ['Feb 2017', 'Mar 2017', 'Mar 2017', 'Jul 2017', 'Sep 2017', 'Feb 2018', 'Mar 2018', 'Apr 2018', 'Apr 2018', 'Jun 2018', 'Sep 2018'],
'Month Closed': ['Apr 2017', 'Apr 2018', '', '', 'Oct 2017', 'Jun 2018', '', 'Feb 2019', 'Aug 2018', '', 'Sep 2018'],
'Case Opened Quarter': ['Q4 2017', 'Q1 2018', 'Q1 2018', 'Q2 2018', 'Q3 2018', 'Q1 2019', 'Q1 2019', 'Q1 2019', 'Q1 2019', 'Q2 2019', 'Q3 2019'],
'Case Closed Quarter': ['Q1 2018', 'Q1 2019', '', '', 'Q3 2018', 'Q2 2019', '', 'Q1 2020', 'Q3 2019', '', 'Q3 2019']
}
df = pd.DataFrame(CaseDetails,columns= ['Case Number', 'Status', 'Date/Time Opened', 'Date/Time Closed', 'VMware Owner', 'Month Opened', 'Month Closed', 'Case Opened Quarter', 'Case Closed Quarter'])
ax = df.groupby(['Case Opened Quarter', 'Month Opened'], sort=False)[
'Case Number'].size().unstack().plot(kind='bar', stacked=True, legend=False)
ax.xaxis.set_label_text("Quarter")
ax.yaxis.set_label_text("Record Count")
ax.set_title("Cases Opened")
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
使用上面的代码,我可以获得这张图。
我要显示的是每个季度每月打开的案件数?可以将它们作为标签显示在图形上吗?
类似地,是否可以将2个图形合并为一个?如果我有2个图表-案件已结案和案件结案,我能否得到一张图表,其中每个堆栈显示每个季度已结案和结案的案件?
上图的代码
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(224)
df.groupby(['Case Opened Quarter', 'Month Opened'], sort=False)[
'Case Number'].size().unstack().plot(kind='bar', stacked=True, ax=ax1, legend=False)
ax1.xaxis.set_label_text("Quarter")
ax1.yaxis.set_label_text("Record Count")
ax1.set_title("Cases Opened")
ax1.yaxis.set_major_locator(MaxNLocator(integer=True))
df.groupby(['Case Closed Quarter', 'Month Closed'], sort=False)[
'Case Number'].size().unstack().plot(kind='bar', stacked=True, ax=ax2, legend=False)
ax2.xaxis.set_label_text("Quarter")
ax2.yaxis.set_label_text("Record Count")
ax2.set_title("Cases Closed")
ax2.yaxis.set_major_locator(MaxNLocator(integer=True))
我在使用this answer方面取得了一些进展。我能够得到堆积条上的计数,但它们都被弄乱了。
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(224)
df.groupby(['Case Opened Quarter', 'Month Opened'], sort=False)[
'Case Number'].size().unstack().plot(kind='bar', stacked=True, ax=ax1, legend=False)
ax1.xaxis.set_label_text("Quarter")
ax1.yaxis.set_label_text("Record Count")
ax1.set_title("Cases Opened")
ax1.yaxis.set_major_locator(MaxNLocator(integer=True))
df.groupby(['Case Closed Quarter', 'Month Closed'], sort=False)[
'Case Number'].size().unstack().plot(kind='bar', stacked=True, ax=ax2, legend=False)
ax2.xaxis.set_label_text("Quarter")
ax2.yaxis.set_label_text("Record Count")
ax2.set_title("Cases Closed")
ax2.yaxis.set_major_locator(MaxNLocator(integer=True))
for p in ax1.patches:
ax1.annotate(p.get_height(), (p.get_x() + p.get_width() / 2, p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')
for p in ax2.patches:
ax2.annotate(p.get_height(), (p.get_x() + p.get_width() / 2, p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')
这是图形的外观
对于更复杂的数据,图像变得更加混乱。