【问题标题】:matplotlib normed histogramsmatplotlib 规范直方图
【发布时间】:2012-08-30 07:40:05
【问题描述】:

我正在尝试使用 matplotlib 绘制直方图的一部分。

我不想绘制包含大量异常值和较大值的整个直方图,而是只关注一小部分。原始直方图如下所示:

hist(data, bins=arange(data.min(), data.max(), 1000), normed=1, cumulative=False)
plt.ylabel("PDF")

对焦后是这样的:

hist(data, bins=arange(0, 121, 1), normed=1, cumulative=False)
plt.ylabel("PDF")

请注意,最后一个 bin 被拉伸,所有 Y 刻度中最差的一个被缩放,因此总和正好为 1(因此根本不考虑当前范围之外的点)

我知道我可以通过在整个可能范围内绘制直方图然后将轴限制到我感兴趣的部分来实现我想要的,但是这会浪费大量时间来计算我不会使用的 bin /还是看看吧。

hist(btsd-40, bins=arange(btsd.min(), btsd.max(), 1), normed=1, cumulative=False)
axis([0,120,0,0.0025])

有没有一种快速简便的方法来仅绘制焦点区域但仍使 Y 比例正确?

【问题讨论】:

  • 如何在不考虑整个数据集的情况下计算规范值?一般来说,直方图值的计算应使曲线的积分为1,而不是简单地除以点数。
  • 在没有描述分布的函数的情况下,你能做的最好的就是计算点数并相应地划分。

标签: python matplotlib histogram


【解决方案1】:

为了绘制直方图的子集,我认为您无法计算整个直方图。

您是否尝试过使用numpy.histogram 计算直方图,然后使用pylab.plot 或其他方式绘制区域?即

import numpy as np
import pylab as plt

data = np.random.normal(size=10000)*10000

plt.figure(0)
plt.hist(data, bins=np.arange(data.min(), data.max(), 1000))

plt.figure(1)
hist1 = np.histogram(data, bins=np.arange(data.min(), data.max(), 1000))
plt.bar(hist1[1][:-1], hist1[0], width=1000)

plt.figure(2)
hist2 = np.histogram(data, bins=np.arange(data.min(), data.max(), 200))
mask = (hist2[1][:-1] < 20000) * (hist2[1][:-1] > 0)
plt.bar(hist2[1][mask], hist2[0][mask], width=200)

原始直方图:

手动计算直方图:

手动计算的直方图,裁剪: (注意:值更小,因为 bin 更窄)

【讨论】:

    【解决方案2】:

    我认为,您可以使用给定的权重对数据进行标准化。 (repeat 是一个 numpy 函数)。

    hist(data, bins=arange(0, 121, 1), weights=repeat(1.0/len(data), len(data)))

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 2011-07-16
      • 1970-01-01
      • 2012-08-20
      • 2019-07-07
      • 1970-01-01
      相关资源
      最近更新 更多