Python 堆叠条形图:条形图和标签的高度不正确

Python 堆叠条形图:条形图和标签的高度不正确,python,matplotlib,Python,Matplotlib,简而言之: 钢筋的高度与编号不匹配 标签似乎放错了高度。(应该在每个栏的中间) 在最底部,我还看到了“0”标签,我真的不想在图中看到这些标签 解释: 我试图制作一个堆叠的条形图,并在每个条形图上标注相应的值。但由于某种原因,这些栏杆的高度是完全错误的。就像第一周一样,绿条应该有20点长,但只有10点。红条应该有10点长,但只有8点左右。第17周应该有多个条,但只有一个(白色条) 我猜,由于酒吧的高度不对,标签也放错地方了。我不知道为什么底部的0也会出现,但这也是一个问题 我不知道这些是否都是

简而言之:

  • 钢筋的高度与编号不匹配
  • 标签似乎放错了高度。(应该在每个栏的中间)
  • 在最底部,我还看到了“0”标签,我真的不想在图中看到这些标签
解释:

我试图制作一个堆叠的条形图,并在每个条形图上标注相应的值。但由于某种原因,这些栏杆的高度是完全错误的。就像第一周一样,绿条应该有20点长,但只有10点。红条应该有10点长,但只有8点左右。第17周应该有多个条,但只有一个(白色条)

我猜,由于酒吧的高度不对,标签也放错地方了。我不知道为什么底部的0也会出现,但这也是一个问题

我不知道这些是否都是独立的问题,应该在不同的帖子中提问,但我觉得它们都是相互关联的,有一个答案可以解决所有问题

import matplotlib.pyplot as plt
import numpy as np


newYearWeek =[201613, 201614, 201615, 201616, 201617, 201618, 201619, 201620, 201621, 201622]
uniqueNames = ['Word1', 'Word2', 'Word3', 'Word4', 'Word5', 'Word6',
                'Word7', 'Word8', 'Word9', 'Word10', 'Word11']

#Each column in the multiarray from top to bottom represents 1 week
#Each row from left to right represents the values of that word.
#So that makes 11 rows and 10 columns.
#And yes the multidimensional array have to be like this with the 0's in it.
keywordsMuliarray = [
    [20, 3, 1, 0, 0, 1, 6, 3, 1, 2],
    [10, 1, 0, 0, 3, 1, 3, 1, 0, 2],
    [2, 2, 5, 3, 5, 4, 5, 4, 3, 2],
    [0, 4, 3, 3, 1, 0, 2, 7, 1, 2],
    [0, 0, 2, 0, 1, 1, 1, 0, 1, 3],
    [0, 0, 3, 2, 0, 0, 0, 1, 0, 0],
    [1, 0, 1, 0, 1, 0, 0, 0, 1, 1],
    [0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
    [0, 1, 0, 0, 7, 6, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 2, 0, 1]]

fig = plt.figure(figsize=(8.5, 5.5))
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)

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

colors = ['seagreen', 'indianred', 'steelblue', 'darkmagenta', 'wheat',
           'orange', 'mediumslateblue', 'silver',
           'whitesmoke', 'black', 'darkkhaki', 'dodgerblue', 'crimson',
           'sage', 'navy', 'plum', 'darkviolet', 'lightpink']

def autolabel(rects, values):
    # Attach some text labels.
    for (rect, value) in zip(rects, values):
        ax.text(rect.get_x() + rect.get_width() / 2.,
                rect.get_y() + rect.get_height() / 2.,
                '%d'%value,
                ha = 'center',
                va = 'center')
left = np.zeros(len(uniqueNames)) # left alignment of data starts at zero
helpingNumber = 0
for i in range(0, len(newYearWeek)):
    rects1 = plt.bar(ind, keywordsMuliarray[helpingNumber][:],width, color=colors[helpingNumber], label=uniqueNames[helpingNumber])
    autolabel(rects1, keywordsMuliarray[helpingNumber][:])
    helpingNumber = helpingNumber+1

# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 1, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5))

#plt.ylabel('Scores')
plt.xticks(ind + width/2., newYearWeek, fontsize=8)
#plt.yticks(np.arange(0, 81, 10))
plt.margins(x=0.02)
plt.tight_layout(rect=[0,0,0.8,1])
plt.show()
这是图表现在的样子:

若要生成所需的值,必须将当前列中所有先前条的高度相加(列出
bot\u heights
),如下所示:

import matplotlib.pyplot as plt
import numpy as np


newYearWeek =[201613, 201614, 201615, 201616, 201617, 201618, 201619, 201620, 201621, 201622]
uniqueNames = ['Word1', 'Word2', 'Word3', 'Word4', 'Word5', 'Word6',
                'Word7', 'Word8', 'Word9', 'Word10', 'Word11']

#Each column in the multiarray from top to bottom represents 1 week
#Each row from left to right represents the values of that word.
#So that makes 11 rows and 10 columns.
#And yes the multidimensional array have to be like this with the 0's in it.
keywordsMuliarray = [
    [20, 3, 1, 0, 0, 1, 6, 3, 1, 2],
    [10, 1, 0, 0, 3, 1, 3, 1, 0, 2],
    [2, 2, 5, 3, 5, 4, 5, 4, 3, 2],
    [0, 4, 3, 3, 1, 0, 2, 7, 1, 2],
    [0, 0, 2, 0, 1, 1, 1, 0, 1, 3],
    [0, 0, 3, 2, 0, 0, 0, 1, 0, 0],
    [1, 0, 1, 0, 1, 0, 0, 0, 1, 1],
    [0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
    [0, 1, 0, 0, 7, 6, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 2, 0, 1]]

fig = plt.figure(figsize=(8.5, 5.5))
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)

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

colors = ['seagreen', 'indianred', 'steelblue', 'darkmagenta', 'wheat',
           'orange', 'mediumslateblue', 'silver',
           'whitesmoke', 'black', 'darkkhaki', 'dodgerblue', 'crimson',
           'sage', 'navy', 'plum', 'darkviolet', 'lightpink']

def autolabel(rects, values):
    # Attach some text labels
    for (rect, value) in zip(rects, values):
        if value > 0:
            ax.text(rect.get_x() + rect.get_width() / 2.,
             rect.get_y() + rect.get_height() / 2.,
             '%d'%value, ha = 'center', va = 'center', size = 9)

left = np.zeros(len(uniqueNames)) # left alignment of data starts at zero

# plot the first bars
rects1 = plt.bar(ind, keywordsMuliarray[0][:],width,
 color=colors[0], label=uniqueNames[0])
autolabel(rects1, keywordsMuliarray[0][:])

# put other bars on previuos
bot_heights = [0.] * len(keywordsMuliarray[0][:])
for i in xrange(1,N):
    bot_heights = [bot_heights[j] + keywordsMuliarray[i-1][j] for j in xrange(len(bot_heights))]

    rects1 = plt.bar(ind, keywordsMuliarray[i][:],width,
     color=colors[i], label=uniqueNames[i],
     bottom=bot_heights)
    autolabel(rects1, keywordsMuliarray[i][:])

# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 1, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', fontsize=9, bbox_to_anchor=(1, 0.5))

#plt.ylabel('Scores')
plt.xticks(ind + width/2., newYearWeek, fontsize=8)
plt.yticks(np.arange(0, 41, 5))
plt.margins(x=0.02)
plt.tight_layout(rect=[0,0,0.8,1])
plt.show()
为防止条形标签重叠,如果值为零,建议不要添加标签(请查看修改的
autolabel
函数)。因此,我得到:


您的代码没有将您的条堆叠在彼此的顶部;这是重叠的。看看这个例子,更好地了解如何制作堆叠条形图:是的,我也是这么想的。我想我需要使用“bottom=”关键字。但是我不知道如何将它实现到for循环中,在这个循环中,每次运行它时,图形中的字数都可能不同。此外,即使我可以实现它(由于某些原因,我似乎无法实现),我仍然不知道这是否解决了标签问题。:/关键字“bottom=”是否会导致图表中的0值出现问题?(只是猜测)