绘制两个直方图 - 为什么它们绘制在同一个 X 列上?

Plotting two histograms over each other - why are they plotting on the same X column?

我有两个浮动列表。我可以看到两个列表中的浮点数在0.003到0.99之间。

列表太长,无法粘贴到此处,但它们看起来像这样:

list1 = [0.03, 0.4, 0.4, 0.5, 0.5, 0.6, 0.7, 0.993]
list2 = [0.4, 0.3, 0.2, 0.7, 0.002, 0.4, 0.01, 0.3]

我可以看到列表中肯定有从 <0.1 到 >0.9 的值。

当我 运行 此代码将这两个列表绘制为一个图上的直方图时:

import matplotlib.pyplot as plt
plt.hist(list1, alpha=0.5)
plt.hist(list2,alpha=0.5)
plt.savefig('test_histogram.png')

输出如下所示:

但是没有错误。肯定有很多值 <0.8,所以这不仅仅是因为值太小以至于看不到。有人可以解释发生了什么吗?

只需将 'histtype' 作为 bar

plt.hist(list1, alpha=0.5, density=True, histtype='bar')

plt.hist(list2,alpha=0.5, density=True, histtype='bar')

plt.show()`

输出: