【发布时间】:2018-09-21 17:50:36
【问题描述】:
我想用 Matplotlib 绘制直方图,但我希望 bin 的值代表总观察值的百分比。 MWE 应该是这样的:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
import seaborn as sns
import numpy
sns.set(style='dark')
imagen2 = plt.figure(1, figsize=(5, 2))
imagen2.suptitle('StackOverflow Matplotlib histogram demo')
luminance = numpy.random.randn(1000, 1000)
# "Luminance" should range from 0.0...1.0 so we normalize it
luminance = (luminance - luminance.min())/(luminance.max() - luminance.min())
top_left = plt.subplot(121)
top_left.imshow(luminance)
bottom_left = plt.subplot(122)
sns.distplot(luminance.flatten(), kde_kws={"cumulative": True})
# plt.savefig("stackoverflow.pdf", dpi=300)
plt.tight_layout(rect=(0, 0, 1, 0.95))
plt.show()
这里的 CDF 还可以(范围:[0, 1]),但是生成的直方图不符合我的预期:
为什么直方图的结果在 [0, 4] 范围内?有没有什么办法解决这一问题?
【问题讨论】:
-
直方图实际上是已经归一化的,但就其密度而言。本质上,
sum(bin_heights*bin_widths) == 1.0 -
如果您真的希望 bin 高度总和为 1.0,您也可以使用
numpy.histogram函数自己计算它们。我在下面的答案中添加了一个示例
标签: python matplotlib histogram seaborn