matplotlib 堆积条形图不绘制总值
matplotlib stacked bar chart does not plot the total values
我有一个名为性别的数组,它存储男性和女性的销售额。我正在尝试创建这些项目的堆积条形图,但该图显示 y 轴上 7 处的堆积条形图正在更改为下一个堆栈,在 y 轴上上升到 14,但应该上升到 28。
gender = [[7],[14]]
attributes = ("Male & Female")
y_pos = np.arange(len(attributes))
plt.bar(y_pos, gender[0], align='center', alpha=0.5)
plt.bar(y_pos, gender[1], align='center', alpha=0.5)
plt.xticks(y_pos, attributes)
plt.ylabel('Volume')
plt.xlabel('Customer Attributes')
plt.title('Number of Customer Types')
plt.show()
我不确定我是否正确理解了你的问题。但是,如果您希望第二个柱位于第一个柱的顶部,您可以通过将第二个柱的 bottom
参数设置为第一个柱的高度来实现,如下所示:
gender = [(7),(14)]
attributes = ["Male & Female"]
y_pos = np.arange(len(attributes))
plt.bar(y_pos, gender[0], align='center', alpha=0.5)
plt.bar(y_pos, gender[1], align='center', alpha=0.5, bottom=gender[0])
plt.xticks(y_pos, attributes)
plt.ylabel('Volume')
plt.xlabel('Customer Attributes')
plt.title('Number of Customer Types')
plt.show()
我有一个名为性别的数组,它存储男性和女性的销售额。我正在尝试创建这些项目的堆积条形图,但该图显示 y 轴上 7 处的堆积条形图正在更改为下一个堆栈,在 y 轴上上升到 14,但应该上升到 28。
gender = [[7],[14]]
attributes = ("Male & Female")
y_pos = np.arange(len(attributes))
plt.bar(y_pos, gender[0], align='center', alpha=0.5)
plt.bar(y_pos, gender[1], align='center', alpha=0.5)
plt.xticks(y_pos, attributes)
plt.ylabel('Volume')
plt.xlabel('Customer Attributes')
plt.title('Number of Customer Types')
plt.show()
我不确定我是否正确理解了你的问题。但是,如果您希望第二个柱位于第一个柱的顶部,您可以通过将第二个柱的 bottom
参数设置为第一个柱的高度来实现,如下所示:
gender = [(7),(14)]
attributes = ["Male & Female"]
y_pos = np.arange(len(attributes))
plt.bar(y_pos, gender[0], align='center', alpha=0.5)
plt.bar(y_pos, gender[1], align='center', alpha=0.5, bottom=gender[0])
plt.xticks(y_pos, attributes)
plt.ylabel('Volume')
plt.xlabel('Customer Attributes')
plt.title('Number of Customer Types')
plt.show()