Python Pandas: Plotting 100% stacked graph issue -
i got dataframe df5 following table read in read_csv,
week_days,category,total_products_sold,total_profit 0.monday,a,3221,9999.53 0.monday,b,1038,26070.33 0.monday,c,699,13779.56 0.monday,e,3055,18157.26 0.monday,f,47569,215868.15 0.monday,g,2348,23695.25 0.monday,h,6,57 0.monday,i,14033,64594.24 0.monday,j,13876,47890.91 0.monday,k,3878,14119.74 0.monday,l,243,2649.6 0.monday,m,2992,16757.38 1.tuesday,a,2839,8864.78 1.tuesday,b,1013,26254.69 1.tuesday,c,656,13206.98 1.tuesday,e,2696,15872.45 1.tuesday,f,43039,197621.18 1.tuesday,g,2107,21048.72 1.tuesday,h,3,17 1.tuesday,i,12297,56942.99 1.tuesday,j,12095,40724.2 1.tuesday,k,3418,12551.26 1.tuesday,l,243,2520.3 1.tuesday,m,2375,13268.28 2.wednesday,a,2936,9119.93 2.wednesday,b,1061,26927.86 2.wednesday,c,634,10424.05 2.wednesday,e,2835,16627.35 2.wednesday,f,46128,218014.59 2.wednesday,g,1986,19173.64 4.friday,h,24,233 4.friday,i,17576,81648.75 4.friday,j,16468,55820.9 4.friday,k,4294,16603.39 4.friday,l,440,4258.51 4.friday,m,3600,20142.44 5.saturday,a,4658,15051.13 5.saturday,b,1492,38236.07 5.saturday,c,1057,15449.7 5.saturday,e,5335,29904.96 5.saturday,f,79925,362120.61 5.saturday,g,4324,44088.79 5.saturday,h,26,933 5.saturday,i,22688,106313.86 5.saturday,j,21882,74725.11 5.saturday,k,5402,20875.84 5.saturday,l,458,4692.84 5.saturday,m,4896,27769.68 6.sunday,a,3429,11310.1 6.sunday,b,1104,27282.99 6.sunday,c,1051,11567.08 6.sunday,e,3913,22740.63 6.sunday,f,56048,259105.03 6.sunday,g,3224,32528.39 6.sunday,h,21,749 6.sunday,i,15853,74876.77 6.sunday,j,16072,55259.76 6.sunday,k,4383,16058.36 6.sunday,l,327,3348.82 6.sunday,m,3551,20814.05
i want plot 2 100% stacked bar charts total products sold , total profit each, x-axis week days , labels different categories.
my code total products sold is
df5 = df5.set_index(['week_days', 'category']) df5 = df5.div(df5.sum(1), axis=0) ax = df5[['total_products_sold']].plot(kind='bar', stacked=true, width = 0.3, figsize=(20, 10), colormap="rdbu") patches, labels = ax.get_legend_handles_labels() ax.legend(bbox_to_anchor=(1.1, 1.0)) ax.set_xlabel('week days') ax.set_ylabel('products sold')
the graph got returned looks nothing need. not 100 stacked , legend total products sold , not different categories in category.
can please help? thanks.
regards, lobbie
the easiest way make pivot table values care about. try this:
tps = df5.pivot_table(values=['total_products_sold'], index='week_days', columns='category', aggfunc='sum') tps = tps.div(tps.sum(1), axis=0) tps.plot(kind='bar', stacked=true)
for me produces following:
you can same thing total_profit
separately.
Comments
Post a Comment