【发布时间】:2011-04-21 11:01:00
【问题描述】:
我想使用matplotlib 从向量中绘制归一化直方图。我尝试了以下方法:
plt.hist(myarray, normed=True)
还有:
plt.hist(myarray, normed=1)
但是这两个选项都不会从 [0, 1] 生成 y 轴,因此直方图的条形高度总和为 1。
【问题讨论】:
标签: python matplotlib seaborn histogram
我想使用matplotlib 从向量中绘制归一化直方图。我尝试了以下方法:
plt.hist(myarray, normed=True)
还有:
plt.hist(myarray, normed=1)
但是这两个选项都不会从 [0, 1] 生成 y 轴,因此直方图的条形高度总和为 1。
【问题讨论】:
标签: python matplotlib seaborn histogram
seaborn.histplot,或seaborn.displot 和kind='hist',并指定stat='probability'
data:pandas.DataFrame、numpy.ndarray、映射或序列seaborn 是 matplotlib 的高级 API
python 3.8.12、matplotlib 3.4.3、seaborn 0.11.2 中测试import seaborn as sns
import matplotlib.pyplot as plt
# load data
df = sns.load_dataset('penguins')
sns.histplot# create figure and axes
fig, ax = plt.subplots(figsize=(6, 5))
p = sns.histplot(data=df, x='flipper_length_mm', stat='probability', ax=ax)
sns.displotp = sns.displot(data=df, x='flipper_length_mm', stat='probability', height=4, aspect=1.5)
【讨论】:
自 matplotlib 3.0.2 起,normed=True 已弃用。为了获得所需的输出,我必须这样做:
import numpy as np
data=np.random.randn(1000)
bins=np.arange(-3.0,3.0,51)
counts, _ = np.histogram(data,bins=bins)
if density: # equivalent of normed=True
counts_weighter=counts.sum()
else: # equivalent of normed=False
counts_weighter=1.0
plt.hist(bins[:-1],bins=bins,weights=counts/counts_weighter)
尝试同时指定weights 和density 作为plt.hist() 的参数对我不起作用。如果有人知道如何在没有访问规范关键字参数的情况下使其正常工作,请在 cmets 中告诉我,我将删除/修改此答案。
如果您想要 bin 中心,则不要使用作为 bin 边缘的 bins[:-1] - 您需要选择一个合适的方案来计算中心(可能会或可能不会简单地导出)。
【讨论】:
如果您希望所有条形的总和相等,请按值的总数对每个 bin 加权:
weights = np.ones_like(myarray) / len(myarray)
plt.hist(myarray, weights=weights)
希望对您有所帮助,虽然线程很旧......
Python 2.x 的注意事项:为除法运算符之一添加转换到 float(),否则由于整数除法,您最终会得到零
【讨论】:
array_like 而不是 numpy 数组,则需要将 len(myarray) 转换为 float。
如果您提出一个更完整的工作(或在这种情况下不工作)示例会更有帮助。
我尝试了以下方法:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1000)
fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, rectangles = ax.hist(x, 50, density=True)
fig.canvas.draw()
plt.show()
这确实会生成一个条形图直方图,其 y 轴从 [0,1] 开始。
此外,根据hist 文档(即来自ipython 的ax.hist?),我认为总和也不错:
*normed*:
If *True*, the first element of the return tuple will
be the counts normalized to form a probability density, i.e.,
``n/(len(x)*dbin)``. In a probability density, the integral of
the histogram should be 1; you can verify that with a
trapezoidal integration of the probability density function::
pdf, bins, patches = ax.hist(...)
print np.sum(pdf * np.diff(bins))
在上面的命令之后尝试一下:
np.sum(n * np.diff(bins))
如预期的那样,我得到了1.0 的返回值。请记住,normed=True 并不意味着每个条的值之和是统一的,而是条上的积分是统一的。在我的情况下,np.sum(n) 返回了大约 7.2767。
【讨论】:
这是另一个使用np.histogram() 方法的简单解决方案。
myarray = np.random.random(100)
results, edges = np.histogram(myarray, normed=True)
binWidth = edges[1] - edges[0]
plt.bar(edges[:-1], results*binWidth, binWidth)
您确实可以通过以下方式检查总和是否为 1:
> print sum(results*binWidth)
1.0
【讨论】:
考虑到这个问题的日期是 2010 年,我知道这个答案为时已晚,但我遇到了这个问题,因为我自己也面临着类似的问题。如答案中所述, normed=True 表示直方图下的总面积等于 1,但高度之和不等于 1。但是,为了方便对直方图进行物理解释,我想制作一个高度之和等于 1。
我在以下问题中找到了提示 - Python: Histogram with area normalized to something other than 1
但我无法找到使条形图模仿 histtype="step" 功能 hist() 的方法。这让我转向:Matplotlib - Stepped histogram with already binned data
如果社区认为它可以接受,我想提出一个综合上述两个帖子的想法的解决方案。
import matplotlib.pyplot as plt
# Let X be the array whose histogram needs to be plotted.
nx, xbins, ptchs = plt.hist(X, bins=20)
plt.clf() # Get rid of this histogram since not the one we want.
nx_frac = nx/float(len(nx)) # Each bin divided by total number of objects.
width = xbins[1] - xbins[0] # Width of each bin.
x = np.ravel(zip(xbins[:-1], xbins[:-1]+width))
y = np.ravel(zip(nx_frac,nx_frac))
plt.plot(x,y,linestyle="dashed",label="MyLabel")
#... Further formatting.
这对我来说非常有效,尽管在某些情况下我注意到直方图最左边的“条”或最右边的“条”不会通过触摸 Y 轴的最低点而关闭。在这种情况下,在 begging 或 y 末尾添加元素 0 即可获得必要的结果。
只是想我会分享我的经验。谢谢。
【讨论】: