如何在python中绘制重复堆积条形图?

时间:2018-05-25 20:46:20

标签: python matplotlib stacked-chart

我正在尝试绘制重复颜色的水平堆积条形图。但我无法做到!

这是我的开始代码:     导入numpy为np     从matplotlib导入pyplot作为plt

dataset = [{'A':19, 'B':55},
           {'A':10, 'B':68}]
data_orders = [['B', 'A'],
               ['A', 'B']]
colors = ["green","blue"]
names = sorted(dataset[0].keys())
values = np.array([[data[name] for name in order] for data,order in 
zip(dataset, data_orders)])
lefts = np.insert(np.cumsum(values, axis=1),0,0, axis=1)[:, :-1]
orders = np.array(data_orders)
bottoms = np.arange(len(data_orders))

for name, color in zip(names, colors):
    idx = np.where(orders == name)
    value = values[idx]
    left = lefts[idx]
    plt.bar(left=left, height=0.8, width=value, bottom=bottoms,
            color=color, orientation="horizontal", label=name)
plt.legend(loc="best", bbox_to_anchor=(1.0, 1.00))
plt.subplots_adjust(right=0.85)
plt.show()

输出: enter image description here

但我想要的是: enter image description here

并希望提供带有标签 - 值对的数据集,并获得第二个输出数字。

也许我的代码是一个糟糕的开始! 任何帮助都将非常非常有帮助!谢谢......

0 个答案:

没有答案