python - 有没有办法在 numpy.hist 中返回相同长度的数组?

标签 python arrays numpy plot histogram

我正在尝试在 python 中创建直方图,使用一些自定义值对 y 轴值进行归一化。为此,我想这样做:

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('foo.bar')
fig = plt.figure()
ax = fig.add_subplot(111)

hist=np.histogram(data, bins=(1.0, 1.5 ,2.0,2.5,3.0))
x=[hist[0]*5,hist[1]]
ax.plot(x[0], x[1], 'o')

当然,最后一行给出了:

ValueError: x and y must have same first dimension

有没有办法强制 np.hist 为 x[0] 和 x[1] 数组提供相同数量的元素,例如删除其中一个数组的第一个或最后一个元素?

最佳答案

hist[1] 包含您制作直方图的范围。我想您可能想要获得这些间隔的中心,例如:

x = [hist[0], 0.5*(hist[1][1:]+hist[1][:-1])]

然后剧情应该没问题吧?

关于python - 有没有办法在 numpy.hist 中返回相同长度的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17966093/

相关文章:

python - 混合形状阵列列的点积

numpy - 奇数大小的 numpy 数组发送/接收

python - 屏蔽 numpy 数组中的圆形扇区

python - 无法在 Windows 中安装 ionic

python - 聚合行 Pandas

python - 如果测试没有足够的 python 覆盖率,如何让 travis 失败

php - 如何按条件过滤数组

javascript - 将每个单词推送到一个数组

arrays - const 结构成员的 typedef 中的灵活数组大小

python - xtensor 类型的性能与 NumPy 的简单归约