Numpy直方图数据:为什么bins向量的长度比直方图值向量长?

2024-03-29 12:45:33 发布

您现在位置:Python中文网/ 问答频道 /正文

有两个输出到numpy.histogram

  • hist:直方图的值
  • bin_edges:返回箱子边缘(length(hist)+1)

两者都是向量,但在下面的示例中,第二个向量的长度为101,比第一个向量的长度为100高1:

import numpy as np
from numpy.random import rand, randn

n = 100  # number of bins
X = randn(n)*.1
a,bins1 = np.histogram(X,bins=n)

如果我尝试plt.plot(bins1,a),则会出现以下形状错误:

ValueError: x and y must have same first dimension, but have shapes (101,) and (100,)

为什么,以及如何修复不相等的形状错误,以便绘制直方图


Tags: andimportnumpybinhave错误np直方图