python - 更改 Python 直方图 bin 中的计数

标签 python matplotlib histogram

我有一个 Python 直方图。

我想将直方图的峰值归一化为 1,这样只有条形的相对高度很重要。

我看到一些涉及更改 bin 宽度的方法,但我不想这样做。

我还意识到我可以只更改 y 轴的标签,但我还覆盖了另一个图,因此 yticks 必须是实际值。

没有办法访问和更改每个 bin 中的直方图“计数”吗?

谢谢。

最佳答案

我认为您想要的是直方图的标准化形式,其中 y 轴是密度而不是计数。如果您使用的是 Numpy,只需在 histogram function 中使用 normed 标志.

如果您希望直方图的峰值为 1,则可以将每个 bin 中的计数除以最大 bin 值,即(构建 SO MatPlotLib 示例 here):

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

# Generate random data
mu, sigma = 200, 25
x = mu + sigma*np.random.randn(10000)

# Create the histogram and normalize the counts to 1
hist, bins = np.histogram(x, bins = 50)
max_val = max(hist)
hist = [ float(n)/max_val for n in hist]

# Plot the resulting histogram
center = (bins[:-1]+bins[1:])/2
width = 0.7*(bins[1]-bins[0])
plt.bar(center, hist, align = 'center', width = width)
plt.show()

enter image description here

关于python - 更改 Python 直方图 bin 中的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19139232/

相关文章:

python - 带有灰度数据的 Enthought Canopy Chaco img_plot

python - NetworkX 获取应用权重的节点的度数

python - 如何修复图例中的线条大小以匹配在 matplotlib 版本 2.00 中使用 `LineCollection` 的图中的线条?

python - 如何将 matplotlib 的 ggplot 风格与 agg 后端一起使用

c++ - 直方图均衡的属性?在没有人工制品的情况下增强对比度?

python - 如何创建字典中值出现的直方图?

python - (Python) 属性错误 : 'NoneType' object has no attribute 'text'

python - 如何在贷款计算器中获得每月分期付款?

python - 如何将 matplotlib (python) 窗口保留在后台?

python - python中的多维直方图