【问题标题】:Get two histograms plotted over eachother to have the same width bins获取两个相互绘制的直方图以具有相同宽度的 bin
【发布时间】:2020-05-06 12:01:30
【问题描述】:

我有两个相互绘制的直方图。样本 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 数量,这很好。有没有办法做到这一点?

【问题讨论】:

标签: python numpy matplotlib histogram


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多