gpt4 book ai didi

Python Matplotlib 直方图 bin 移位

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:44 28 4
gpt4 key购买 nike

我已经根据我想要的列表创建了一个累积 (CDF) 直方图。然后我从列表中的每个元素中减去一个固定值(通过使用 x = [ fixed_value - i for i in myArray] ),基本上只是将 bin 移动固定数量。然而,这使我的 CDF 直方图在 y 轴上反转。我认为它看起来应该与原始版本相同,只是 x 轴(bin)移动了固定量。

那么有人可以解释我做错了什么,或者给出一个解决方案,只是将 bin 移过来,而不是用新数组重新创建另一个直方图吗?

编辑:

有时我会看到这个错误:

>>> plt.hist(l,bins, normed = 1, cumulative = True)
C:\Python27\lib\site-packages\matplotlib\axes.py:8332: RuntimeWarning: invalid value encountered in true_divide
m = (m.astype(float) / db) / m.sum()

但不限于第二种减法情况。 plt.hist 返回一个 NaN 数组。不确定这是否有帮助,但我认为我越来越接近弄清楚了。

编辑:这是我的两个图表。第一个是“好”的。第二个是移位的“坏”:

我想要做的就是将第一个垃圾箱移动一个固定的数量。但是,当我从直方图中的每个列表中减去相同的值时,它似乎改变了 y 方向和 x 方向的直方图。另外,请注意第一个直方图都是负值,而第二个是正值。我似乎通过保持负数来修复它(我使用 original_array[i] - fixed_value <0 而不是 fixed_value - original_array[i] > 0 )

最佳答案

我认为问题可能在于您如何计算偏移值。这个例子对我来说很好用:

import numpy as np
import matplotlib.pylab as pl

original_array = np.random.normal(size=100)
bins = np.linspace(-5,5,11)

pl.figure()
pl.subplot(121)
pl.hist(original_array, bins, normed=1, cumulative=True, histtype='step')

offset = -2
modified_array = [original_value + offset for original_value in original_array]

pl.subplot(122)
pl.hist(modified_array, bins, normed=1, cumulative=True, histtype='step')

enter image description here

请注意,numpy 可能会让您的生活更轻松(对于大尺寸的 original_array很多 更快);例如,如果您的数据是 numpy.array,您也可以将其写为:

modified_array = original_array + offset

关于Python Matplotlib 直方图 bin 移位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37795540/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com