Matplotlib 如何在多个条形图分组的情况下绘制条形图?

Matplotlib 如何在多个条形图分组的情况下绘制条形图?,matplotlib,Matplotlib,我想以这样一种方式来绘制我的数据,即将条形图分组?类似这样: 到目前为止,我的代码如下所示: data = NP.genfromtxt('newfile',unpack=True,names=True,dtype=None) for i in sample: mask = data['name'==sample] ax2.bar(pos-0.5,(data['data']*100,label="samples", color="lightblue") 不过,这创建了几个图形,而不是一个组

我想以这样一种方式来绘制我的数据,即将条形图分组?类似这样:

到目前为止,我的代码如下所示:

data = NP.genfromtxt('newfile',unpack=True,names=True,dtype=None)
for i in sample:
 mask = data['name'==sample]
 ax2.bar(pos-0.5,(data['data']*100,label="samples", color="lightblue")

不过,这创建了几个图形,而不是一个组合的图形。如何将其转换为类似于我上面介绍的图形?

在没有列出所有代码的情况下,我必须对数据和您想要的内容进行一些假设。因此,我没有用实际数据测试这段代码。我试图在下面的代码中仔细记录我的假设。如果您对我发布的内容有问题,也许您可以发布代码第一行中提到的文本文件

# Assume data is a record array
data = NP.genfromtxt('newfile',unpack=True,names=True,dtype=None)

# Assume 'sample' is a column in the data
sample = NP.unique(data['name'])
num_items = len(sample)
ind = NP.arange(sample)

# The margin can be increased to add more space between the groups of data
margin = 0.05
width = (1.-2.*margin)/num_items

# This list will make each sample data set a different color
# It must be AT LEAST as long as the number of samples
# If not, the extra data won't be plotted
colorList = ['red', 'blue', 'black']
if len(colorList) < num_items:
    print 'Warning: the number of samples exceeds the length of the color list.'

f = plt.figure()

# Assumed the color was supposed to vary with each data set, so I added a list
for s, color in zip(enumerate(sample), colorList):
    num, i = s
    print "plotting: ", i
    # Assumed you want to plot a separate set of bars for each sample, so I changed the mask to 'name'==i
    mask = data['name'==i]
    # The position of the xdata must be calculated for each of the sample data series
    xdata = ind+margin+(num*width)
    # Assumed you wanted to plot the 'data' column where mask is true and then multiply by 100
    # Also assumed the label and color were supposed to vary with each data set
    plt.bar(xdata, data['data'][mask]*100, width, label=i, color=color)
#假设数据是一个记录数组
data=NP.genfromtxt('newfile',unpack=True,names=True,dtype=None)
#假设“sample”是数据中的一列
sample=NP.unique(数据['name'])
num_items=len(样本)
ind=NP.arange(样本)
#可以增加边距以在数据组之间添加更多空间
保证金=0.05
宽度=(1-2.*边距)/num_项目
#此列表将使每个样本数据集具有不同的颜色
#它必须至少与样本数量一样长
#如果没有,则不会绘制额外的数据
颜色列表=[“红色”、“蓝色”、“黑色”]
如果len(颜色列表)