【问题标题】:Histogram output not matching input直方图输出与输入不匹配
【发布时间】:2014-07-03 20:09:49
【问题描述】:

使用matplotlib,我正在尝试从值列表中生成直方图。我的输出看起来像这里显示的链接中的图像:http://i.stack.imgur.com/4bedX.png 但我知道我的列表中没有任何值高于 200,但 x 轴似乎更高。

我将这些值加载到一个列表中,并在图形制作功能中打印该列表只是为了检查它是否具有正确的值。这就是我的代码的样子。我已经预定义了 yax,并将其加载到一个 numpy 数组中。

     myarray = np.asarray(yax)
     plt.hist(myarray, bins=100, histtype='stepfilled')
     plt.xlabel("Bins")
     plt.ylabel("Frequency")
     plt.ylim(0,10)

我的值列表看起来像这样(更大的除外):

[38, 45, 43, 36, 35, 32, 31, 32, 31, 35, 38, 35, 33, 33, 36, 36, 35, 36, 39, 41, 38, 37, 39, 39, 38, 35, 34, 35, 38, 42, 37, 37, 34, 34, 29, 30, 37, 33, 31, 32, 35, 36, 41, 46, 44, 46, 42, 38, 41, 40, 38]

这是我尝试运行程序的实际列表:http://pastebin.com/U1u6SPsA

【问题讨论】:

  • 你能创建一个实际显示问题的小示例数据集吗?
  • 假设您省略了“import matplotlib.pyplot as plt; import numpy as np”,您提供的代码会生成准确的直方图。要么你的完整“yax”数组有问题,要么这里缺少另一个细节。
  • 我无法复制您的图像。通过使用该数组运行您的代码,我看到this
  • 我想我可能只是不明白如何正确创建直方图,因为当我只是复制和粘贴代码时,我的输出与互联网示例匹配。所以我有一个大小为 76000 的列表,我将这些值
  • 您的代码是正确的——如果您的数据集整数确实是 200 的上限,那么它将生成所需的直方图。话虽如此,人们会假设您的数组实际上包含大于 200 的值。我会仔细查看数组(例如,检查 if len([x for x in yax if x >= 200]) > 0)。跨度>

标签: python numpy matplotlib histogram


【解决方案1】:

直方图是正确的。假设您提供的完整数组定义为“yax”,

import matplotlib.pyplot as plt; import numpy as np
myarray = np.asarray(yax)
plt.hist(myarray, bins=100, histtype='stepfilled')
plt.xlabel("Bins")
plt.ylabel("Frequency")
plt.ylim(0,10)
plt.show() # produces the exact histogram you provided at http://i.stack.imgur.com/4bedX.png

len([x for x in yax if x >= 200])
>>> 102
len([x for x in yax if x >= 1000])
>>> 37
len([x for x in yax if x >= 6000])
>>> 13
len([x for x in yax if x >= 7000])
>>> 3 # matches the height of the histogram for the bin @ 7000

经验是仅目视检查是不够的——不要对数据集的组成做出假设。

【讨论】:

  • 哇,我是垃圾。谢谢。我想我一定是在某个时候搞砸了我的数据集。
  • @user3791562 每个人都会在某些时候犯这个错误,不用担心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 2023-04-09
相关资源
最近更新 更多