gpt4 book ai didi

python - matplotlib 如何计算直方图的密度

转载 作者:行者123 更新时间:2023-12-04 11:00:26 25 4
gpt4 key购买 nike

通读 matplotlib plt.hist 文档,有一个可以设置为 true 的密度参数。文档说

density : bool, optional
If ``True``, the first element of the return tuple will
be the counts normalized to form a probability density, i.e.,
the area (or integral) under the histogram will sum to 1.
This is achieved by dividing the count by the number of
observations times the bin width and not dividing by the total
number of observations. If *stacked* is also ``True``, the sum of
the histograms is normalized to 1.

线路 This is achieved by dividing the count by the number of observations times the bin width and not dividing by the total number of observations
我尝试用示例数据复制它。
**Using matplotlib inbuilt calculations** .

ser = pd.Series(np.random.normal(size=1000))
ser.hist(density = 1, bins=100)

**Manual calculation of the density** :

arr_hist , edges = np.histogram( ser, bins =100)
samp = arr_hist / ser.shape[0] * np.diff(edges)
plt.bar(edges[0:-1] , samp )
plt.grid()

两个图在 y 轴刻度上完全不同,有人能指出到底出了什么问题以及如何手动复制密度计算吗?

最佳答案

这是语言上的歧义。这句话

This is achieved by dividing the count by the number of observations times the bin width

需要这样读
This is achieved by dividing (the count) by (the number of observations times the bin width)

IE。
count / (number of observations * bin width)

完整代码:
import numpy as np
import matplotlib.pyplot as plt

arr = np.random.normal(size=1000)

fig, (ax1, ax2) = plt.subplots(2)
ax1.hist(arr, density = True, bins=100)
ax1.grid()


arr_hist , edges = np.histogram(arr, bins =100)
samp = arr_hist / (arr.shape[0] * np.diff(edges))
ax2.bar(edges[0:-1] , samp, width=np.diff(edges) )
ax2.grid()

plt.show()

enter image description here

关于python - matplotlib 如何计算直方图的密度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58834452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com