简体   繁体   English

获取两个相互绘制的直方图以具有相同宽度的 bin

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

I have two histograms that ive plotted overeachother.我有两个相互绘制的直方图。 Sample 1 has 100 objects and sample 2 has 6000 objects, but i've normalised them such that I can compare them.样本 1 有 100 个对象,样本 2 有 6000 个对象,但我已经对它们进行了规范化,以便我可以比较它们。 However is there a way to get it so both the histograms have the same width bins.但是有没有办法让两个直方图都具有相同的宽度。

Code below:下面的代码:

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() 

So I'd like both histograms to have the same bin edges and widths, and I can totally change the number of bins each graph has, thats fine.所以我希望两个直方图都具有相同的 bin 边缘和宽度,并且我可以完全更改每个图的 bin 数量,这很好。 Is there any way to do this?有没有办法做到这一点?

在此处输入图像描述

You can use the range argument of plt.hist .您可以使用plt.histrange参数。 In this case, given your x-range you could use plt.hist(change_sample1, bins=bins, range=(-1, 1), ...) .在这种情况下,给定您的 x 范围,您可以使用plt.hist(change_sample1, bins=bins, range=(-1, 1), ...) And then use the same range for change_sample2 .然后对change_sample2使用相同的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM