【问题标题】:Numpy Histogram over very tiny floats非常小的浮点数上的 Numpy 直方图
【发布时间】:2020-06-29 11:51:09
【问题描述】:

我有一个带有小浮点数的数组,这是一个例外:

[-0.000631510156545283, 0.0005999252334386763, 2.6784775066479167e-05, 
-6.171351407584846e-05, -2.0256783283654057e-05, -5.700196588437318e-05, 
0.0006830172130385885, -7.862102776837944e-06, 0.0008167604859504389, 
0.0004497656945683915, -0.00017132944173890756, -0.00013510823579343265, 
0.00019666267095029728, -9.0271602657355e-06, 0.0005219852103996746, 
4.010928726736523e-05, -0.0005287787999295592, 0.00023883106926381664, 
0.0006348661301799839, 0.0003881285984411852]

(编辑:整个数组包含 ~40k 浮点数)

数字显示测量值随时间的变化,例如+0.0001 表示测量值增加 0.0001。

我想在整个数组上绘制一个直方图。目前,pyplot.hist 创建了一个将所有值插入一个 bin 的图(This image shows the current histogram.,使用以下代码创建(已编辑):

import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 1, figsize=(20,20))
array = [] # floats here
axs.hist(array,bins=10)
axs.set_ylabel("Histogram of temperature/weight ratio")
axs.set_xlabel("Bins")

)。 我想这是因为人数太少了——我就在这里吗?

我尝试使用hist, bins = numpy.histogram() 并绘制它,结果相同。 (按照这个问题here)。

如何在如此小的数字上创建直方图,以便值分布在例如100 个垃圾箱,但不是全部都插入第一个垃圾箱?我需要预处理我的数据吗?

【问题讨论】:

  • 我只是运行你的代码,它会做它应该做的事情。你能放一张你的图表的图片吗?
  • 对于这个小数组,代码确实是正确的。然而,我的最终数组包含约 40k 个数字。我会在我的问题中澄清这一点,谢谢!
  • 我得到了正确的输出,即使有 50k 个数字和小于 10e-7 的值。也许您在为数组赋值时出错。如果您粘贴完整的代码,它可能会有所帮助。
  • MN通过 bins kwarg 和 no 数组手动指定 bin。 IE。 bins=np.arange(-0.01, 0.01, 0.0001)
  • @JodyKlymak ,这解决了我的问题,感谢您的建议!

标签: python numpy matplotlib histogram


【解决方案1】:

对于寻找答案的其他人:

正如 Jody Klymak 在对我的问题的评论中建议的那样,手动指定垃圾箱。 我不需要像我认为必须做的那样进一步预处理数据。

例子:

import matplotlib.pyplot as plt
import bumpy as np

array = [...] # large array with tiny floats

fig, axs = plt.subplots(1, 1, figsize=(20,20))
hist = axs.hist(array, np.arange(-0.01, 0.01, 0.0001)) #numpy to create bins over range
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2011-11-28
    相关资源
    最近更新 更多