python - Stacked Bar Plot from Dataframe in Pandas -
i'm trying make stacked bar graph x-axes customer names, y axes number of calls, , stacks months.
i have made pivot_table looks this:
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 here code
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) 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') and plotting i've tried
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) and
pivot_table.plot(kind='bar',stacked=true) and tried in bokeh (being pretty important plot) with:
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) # of following commented 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) and 3 plotting methods, keep getting error: valueerror: length mismatch: expected axis has 27 elements, new values have 3 elements i've looked valueerror still don't understand what's going on here.
in case, reading in wrong data file.
Comments
Post a Comment