我使用画廊中的示例作为显示收入和支出的情节的基础。 Pandas DataFrame看起来像这样:
exp inc
Wages 19000.00 0.00
Supplies 3723.60 0.00
Party 1557.70 0.00
Admin 987.50 0.00
Adverts 675.00 0.00
Gifts 511.00 0.00
Internet 231.75 0.00
Oddjobs 0.00 8000.00
Sponsorship 0.00 16900.00
Memberfees 0.00 22050.00
我将数据转换为必需的元组并绘制如下:
p1 = plt.bar(ind, Wages, width, color='#FFF7EC')
p2 = plt.bar(ind, Supplies, width, color='#FEE8C8', bottom=Wages)
p3 = plt.bar(ind, Party, width, color='#FDD49E', bottom=Supplies)
p4 = plt.bar(ind, Admin, width, color='#FDBB84', bottom=Party)
p5 = plt.bar(ind, Adverts, width, color='#FC8D59', bottom=Admin)
p6 = plt.bar(ind, Gifts, width, color='#EF6548', bottom=Adverts)
p7 = plt.bar(ind, Internet, width, color='#D7301F', bottom=Gifts)
p8 = plt.bar(ind, Oddjobs, width, color='#F7FCF5', bottom=Internet)
p9 = plt.bar(ind, Sponsorship, width, color='#E5F5E0', bottom=Oddjobs)
p10 = plt.bar(ind, Memberfees, width, color='#C7E9C0', bottom=Sponsorship)
到目前为止,这么好。图像如下所示:
但是我的支出中有10个条,颜色与图例不匹配,总数不正确(exp应为26686,inc为46950)。这是一个错误,还是我搞砸了?
答案 0 :(得分:0)
答案 1 :(得分:0)
堆积条要求您累计添加y值,即:
p1 = plt.bar(x1, y1, width, color='#FFF7EC')
p2 = plt.bar(x2, y2, width, color='#FEE8C8', bottom=y1)
p3 = plt.bar(x3, y3, width, color='#FDD49E', bottom=y1+y2)
您需要添加bottom属性的y值,因此您需要实现类似的pandas解决方案,并获取每行中的值 并添加它们
如果你还要我详细说明,请告诉我。)