将两个直方图绘制在彼此之上以具有相同宽度的 bin

Get two histograms plotted over eachother to have the same width bins

我有两个直方图,它们相互绘制。样本 1 有 100 个对象,样本 2 有 6000 个对象,但我已将它们标准化,以便我可以比较它们。但是,有没有一种方法可以使两个直方图具有相同的宽度箱。

代码如下:

bins=100
plt.hist(change_sample1, bins=bins, color='blue', edgecolor='black', label='Sample1', density=True)
plt.hist(change_sample2, bins=bins, color='red', edgecolor='black', label='Sample2', density=True) 
plt.xlabel('Change in sample size')
plt.ylabel('Proportion origional sample')
plt.xlim(-1, 1)
plt.title('Distribution of change in sample size')
plt.legend()
plt.show() 

所以我希望两个直方图具有相同的 bin 边缘和宽度,而且我可以完全改变每个图表的 bin 数量,这很好。有什么办法吗?

您可以使用 plt.histrange 参数。在这种情况下,给定您的 x 范围,您可以使用 plt.hist(change_sample1, bins=bins, range=(-1, 1), ...)。然后对 change_sample2.

使用相同的范围