[英]Pandas, Matplotlib and stacked bar chart with 3 values
I have the following code representing 3 values I would like to display in a stacked bar chart. 我有以下代码表示要在堆叠条形图中显示的3个值。
print(df.groupby(['project']).sum()['no_test_or_requirement'])
print(df.groupby(['project']).sum()['requirement_only'])
print(df.groupby(['project']).sum()['test_and_requirement'])
width = .5
fig, ax = plt.subplots(figsize=(13,9))
plt.ylabel('Count')
plt.xlabel('Project')
ax.yaxis.grid(linewidth=.5)
p1 = ax.bar(df['project'].unique(), df.groupby(['project']).sum()['no_test_or_requirement'], color='y', width=.5)
p2 = ax.bar(df['project'].unique(), df.groupby(['project']).sum()['requirement_only'], color='b', width=.5)
p3 = ax.bar(df['project'].unique(), df.groupby(['project']).sum()['test_and_requirement'], color='r', width=.5)
fig.legend((p1[0], p2[0], p3[0]), ('No Test or Requirement', 'Requirement Only', 'Test and Requirement'))
plt.show()
The following is the output from the print statements that reflect the three values in question. 以下是打印语句的输出,该输出反映了所讨论的三个值。
project
ENTMQ 3
ENTMQBR 0
RHDM 1
RHPAM 1
Name: no_test_or_requirement, dtype: int64
project
ENTMQ 13
ENTMQBR 3
RHDM 0
RHPAM 0
Name: requirement_only, dtype: int64
project
ENTMQ 5
ENTMQBR 1
RHDM 0
RHPAM 0
Name: test_and_requirement, dtype: int64
My resulting chart will only show two values stacked and only shows "No Test or Requirement" values when the other values aren't present. 我的结果图表将仅显示两个堆积的值,并且在其他值不存在时仅显示“无测试或要求”值。 The values presented are correct;
给出的值是正确的; however, I do expect to see "No Test or Requirement" represented in the ENTMQ and ENTMQBR bars (3 stacked values).
但是,我确实希望在ENTMQ和ENTMQBR条(3个堆叠的值)中看到“无测试或要求”。 What am I missing here?
我在这里想念什么?
This is working for me. 这对我有用。
df2 = df.groupby(['project']).sum()
df2 = df2.drop(columns=['has_requirement', 'has_testcase'])
ax = df2.plot(kind='barh',stacked=True, figsize=(13,9),
color=('r','g','b', 'y'))
ax.set_xlabel('Count')
ax.set_ylabel('Project')
ax.xaxis.grid(linewidth=1)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.