Python Pandas:绘制 100% 堆叠图问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36784336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas: Plotting 100% stacked graph issue
提问by Lobbie
I got a dataframe df5 with the following table which I read in from read_csv,
我得到了一个数据帧 df5,其中包含我从 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 to plot 2 100% stacked bar charts for Total Products Sold and Total Profit each, where the x-axis is Week Days and the labels are the different Categories.
我想分别绘制 2 个 100% 堆积条形图,用于销售总产品和总利润,其中 x 轴是工作日,标签是不同的类别。
My code for 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 I got returned looks nothing I need. It is not 100 stacked and the legend is Total Products Sold and not the different categories in Category.
我返回的图表看起来没什么我需要的。它不是 100 堆叠,图例是销售的产品总数,而不是类别中的不同类别。
Can someone please help? Thanks.
有人可以帮忙吗?谢谢。
Regards, Lobbie
问候, 大堂
回答by hume
The easiest way is to make a pivot table with the values you care about. Try something like 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 this produces the following:
对我来说,这会产生以下结果:
You can do the same thing for Total_Profit
separately.
你可以Total_Profit
单独做同样的事情。