简体   繁体   English

python matplotlib堆叠在一起的条形图

[英]python matplotlib stacked barcharts grouped together

Explination of data: I have a program I am testing which has 3 parts which add to the ability of the program as a whole eg part1 works on 60% of data, part2 on an additional 10% and part3 on another 6%. 数据解释:我正在测试一个程序,该程序包含3个部分,这些部分增加了整个程序的功能,例如part1处理60%的数据,part2处理另外10%的数据,part3处理另外6%的数据。

What I want to do is have stacked bars which show "out of the total amount of data the program worked on green indicates the amount part1 worked on, red part2 and yellow part3". 我要做的是用堆叠的条形显示“程序在绿色上表示的总数据量中,part1表示红色,part2表示黄色,part3表示黄色”。

As I have tested the program on 3 sets of 5 files I want the graph to reflect this by grouping the 5 barcharts from each dataset together so in the end there are 3 parts to the graph which are an inch away from each other and then each barchart has the stacked effect stated above. 当我在3组5个文件上测试了该程序后,我希望图形通过将来自每个数据集的5个条形图分组在一起来反映这一点,因此最后图形中有3个部分彼此相距一英寸条形图具有上述堆叠效果。

I am aiming for the Y axis to be the percentage the files worked and the Y the names of the groups the files are a part of. 我的目标是Y轴表示文件的工作百分比,Y表示文件所属的组的名称。

As for some example data this is what I have been trying with: 至于一些示例数据,这就是我一直在尝试的方法:

meta_part1 = [5, 5.5, 4.67, 6.54, 4.4]
meta_part2 = [3.1, 3.3, 3.9, 3.5, 3.1]
meta_part3 = [1.3,1.4,1.7,2.4,0.89]

trans_part1 = [90,89.5,94.67,96.54,94.4]
trans_part2 = [11.1,11.3,10.9,11.5,12.1]
trans_part3 = [11.3,11.4,11.7,12.4,10.89]

s_part1 = [55,55.5,54.67,56.54,54.4]
s_part2 = [11.1,11.3,10.9,11.5,12.1]
s_part3 = [11.3,11.4,11.7,12.4,10.89]

meta,trans and s are all groups. meta,trans和s都是组。

My failed code so far is here: 到目前为止,我失败的代码在这里:

import matplotlib.pyplot as plt
import numpy as np
import numpy as np
from numpy.random import randn
import pandas as pd
from scipy import stats
import matplotlib as mpl
import seaborn as sns


meta_part1 = [5, 5.5, 4.67, 6.54, 4.4]
meta_part2 = [3.1, 3.3, 3.9, 3.5, 3.1]
meta_part3 = [1.3,1.4,1.7,2.4,0.89]

trans_part1 = [90,89.5,94.67,96.54,94.4]
trans_part2 = [11.1,11.3,10.9,11.5,12.1]
trans_part3 = [11.3,11.4,11.7,12.4,10.89]

s_part1 = [55,55.5,54.67,56.54,54.4]
s_part2 = [11.1,11.3,10.9,11.5,12.1]
s_part3 = [11.3,11.4,11.7,12.4,10.89]

N = 5

ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, meta_part1,   width, color='y', bottom=meta_part2)
p2 = plt.bar(ind, meta_part2, width, color='r', bottom=meta_part3)
p3 = plt.bar(ind, meta_part3, width, color='g')



plt.ylabel('Scores')
plt.title('Difference between stitchers')
plt.xticks(ind+width/3., ('Test1', 'Test2', 'Test3', 'Test4', 'Test5') )
plt.yticks(np.arange(0,10,1))
plt.legend( (p1[0], p2[0], p3[0]), ('Part1', 'Part2', 'Part3') )
ax.autoscale(tight=True)

plt.show()

However this has been useless so far as it only produces bars with 2 stacking parts and the third is merging into the first and I havnt been able to work out how to combine merging into this code. 但是,这一直没用,因为它只产生带有2个堆叠部分的钢筋,而第三个堆叠到第一个堆叠中,而我无法弄清楚如何将合并到此代码中。

You don't define meta_second and meta_total in your snippet here so it's a bit hard to guess what's going on, but I'll take a stab. 您无需在此处的代码段中定义meta_secondmeta_total ,因此很难猜测会发生什么,但是我会采取行动。 You may be adding lists with + to get meta_total , which will append the two lists not add the values together. 您可能会使用+添加列表以获取meta_total ,它将附加两个列表而不将值加在一起。

Here are modifications that work: 以下是有效的修改:

meta_second = meta_part1
meta_total = [meta_part1[i]+val for i, val in enumerate(meta_part2)]
p1 = plt.bar(ind, meta_part1, width, color='y')
p2 = plt.bar(ind, meta_part2, width, color='r', bottom=meta_second)
p3 = plt.bar(ind, meta_part3, width, color='g', bottom=meta_total)

Also, unrelated, but you try to use ax before it is defined. 同样,这无关,但是您尝试在定义ax之前使用ax You should try to stick with one interface or the other (either pyplot or the object-oriented interface). 您应该尝试使用一个接口或另一个接口(pyplot或面向对象的接口)。 They don't like being mixed. 他们不喜欢混在一起。 Personally, the OO-interface is more robust and reliable, I always use that. 就个人而言,OO接口更加健壮和可靠,我经常使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM