matplotlib 堆叠图未按预期工作

问题描述 投票:0回答:1

我正在尝试使用matplotlib的hist函数来制作堆积图。这是我的代码块的样子:

a = np.array([5.2, 6.7, 8.9, 10.5, 4.2, 2.1])
b = np.array([3.1, 2.2, 9.9, 15.5, 3.8, 1.4])
fig,ax = plt.subplots(1,1,figsize=(7,7))
values = np.arange(0, 6, 1)

plt.hist(values, weights=a,bins=5, histtype='barstacked', label='a')
plt.hist(values, weights=b, bins=5, histtype='barstacked',label='b')
plt.legend()
plt.show()

我也尝试过

histtype='bar'
但这似乎也不起作用。 我一定在这里遗漏了一些非常明显的东西,但是这两个历史记录显然没有叠加。

如有任何帮助,我们将不胜感激。

matplotlib histogram
1个回答
0
投票

尝试:

g = plt.hist(values, weights=a, bins=5, label='a')
plt.hist(values, weights=b, bins=5, label='b', bottom=g[0])

输出:

© www.soinside.com 2019 - 2024. All rights reserved.