Python Pandas Stacked Bar Chart x轴标签

时间:2017-09-12 12:49:26

标签: python pandas dataframe plot bar-chart

我有以下数据框:

Months  Region  Open Case ID    Closed Case ID
April   APAC    648888          648888
April   US      157790  
April   UK      221456          221456
April   APAC    425700  
April   US      634156          634156
April   UK      109445  
April   APAC    442459          442459
May     US      218526  
May     UK      317079          317079
May     APAC    458098  
May     US      726342          726342
May     UK      354155  
May     APAC    463582          463582
May     US      511059  
June    UK      97186           97186
June    APAC    681548  
June    US      799169          799169
June    UK      210129  
June    APAC    935887          935887
June    US      518106  
June    UK      69279           69279

我将获得Open Case ID和Closed Case ID的计数:

df = df.groupby(['Months','Region']).count()

我正在尝试复制Excel生成的下面的图表,如下所示:

我将通过以下方式获得以下内容:

df[['Months','Region']].plot.bar(stacked=True, rot=0, alpha=0.5, legend=False)

enter image description here

有没有办法让python生成的图表更靠近Excel生成的图表,就x轴及其标签的分解方式而言?

1 个答案:

答案 0 :(得分:2)

Theres是设计多索引标签here的类似问题的绝佳解决方案。您可以在该解决方案中使用与ax = fig.gca()相同的绘图参数,即

import matplotlib.pyplot as plt

# add_line,label_len,label_group_bar_table from https://stackoverflow.com/a/39502106/4800652

fig = plt.figure()
ax = fig.add_subplot(111)
#Your df.plot code with ax parameter here
df.plot.bar(stacked=True, rot=0, alpha=0.5, legend=False, ax=fig.gca())

labels = ['' for item in ax.get_xticklabels()]
ax.set_xticklabels(labels)
ax.set_xlabel('')
label_group_bar_table(ax, df)
fig.subplots_adjust(bottom=.1*df.index.nlevels)
plt.show()

基于样本数据的输出:

enter image description here