直方图python高度_Python:直方图的面积归一化为非1

1586010002-jmsa.png

Is there a way to tell matplotlib to "normalize" a histogram such that its area equals a specified value (other than 1)?

The option "normed = 0" in

n, bins, patches = plt.hist(x, 50, normed=0, histtype='stepfilled')

just brings it back to a frequency distribution.

解决方案

Just calculate it and normalize it to any value you'd like, then use bar to plot the histogram.

On a side note, this will normalize things such that the area of all the bars is normed_value. The raw sum will not be normed_value (though it's easy to have that be the case, if you'd like).

E.g.

import numpy as np

import matplotlib.pyplot as plt

x = np.random.random(100)

normed_value = 2

hist, bins = np.histogram(x, bins=20, density=True)

widths = np.diff(bins)

hist *= normed_value

plt.bar(bins[:-1], hist, widths)

plt.show()

vekZr.png

So, in this case, if we were to integrate (sum the height multiplied by the width) the bins, we'd get 2.0 instead of 1.0. (i.e. (hist * widths).sum() will yield 2.0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值