Pandas 中 Dataframe 的堆积条形图

Stacked Bar Plot from Dataframe in Pandas

我正在尝试制作一个堆叠条形图,其中 x 轴是客户名称,y 轴是呼叫次数,堆叠是月份。

我做了一个 pivot_table 看起来像这样的:

pivot_table.head(3)

Out[23]: 
Month                      1   2   3   4   5   6   7   8   9   10  11  12
CompanyName                                                              
Company1   11   3   2   3   5   7   3   6   8   3   5   8
Company2   3   1   2  18   3   4   5   4   5   5   3   2
Company3   2   6   1   3   2   0   5   6   4   8   4   7

这是代码

df = pd.read_csv('MYDATA.csv')
df = df.set_index('recvd_dttm')
df.index = pd.to_datetime(df.index, format='%m/%d/%Y %H:%M')

result = df.groupby([lambda idx: idx.month, 'CompanyName']).agg(len).reset_index()
result.columns = ['Month', 'CompanyName', 'NumberCalls']
pivot_table = result.pivot(index='Month', columns='CompanyName', values='NumberCalls').fillna(0)
s = pivot_table.sum().sort(ascending=False,inplace=False)
pivot_table = pivot_table.ix[:,s.index[:40]]
pivot_table = pivot_table.transpose()



pivot_table = pivot_table.reset_index()
pivot_table['CompanyName'] = [str(x) for x in pivot_table['CompanyName']]
Companies = list(pivot_table['CompanyName'])
months = ["1","2","3","4","5","6","7","8","9","10","11","12"]
pivot_table = pivot_table.set_index('CompanyName')

为了绘图我已经尝试了

ax = pivot_table.plot(kind='bar', title ="Bar chart",figsize=(15,10),legend=True, fontsize=12)
ax.set_xlabel("Company",fontsize=12)
ax.set_ylabel("Number of Calls",fontsize=12)

pivot_table.plot(kind='bar',stacked=True)

并尝试在散景中做到这一点(对于这个情节来说漂亮很重要):

months = OrderedDict(Jan=Jan, Feb=Feb, Mar=Mar, Apr=Apr, 

May=May,Jun=Jun,Jul=Jul,Aug=Aug,Sep=Sep,Oct=Oct,Nov=Nov,Dec=Dec)


# any of the following commented are also alid Bar inputs
#medals = pd.DataFrame(medals)
#medals = list(medals.values())

output_file("stacked_bar.html")

bar = Bar(months, Companies, title="Stacked bars", stacked=True)

show(bar)

并且对于所有三种绘图方法,不断收到此错误:ValueError: Length mismatch: Expected axis has 27 elements, new values have 3 elements我已经查看了这个 ValueError,但我仍然不明白这里发生了什么。

就我而言,我读入了错误的数据文件。