箱子必须单调增加

bins must increase monotonically

我只想从 skimage.exposure 绘制 Matplotlib 直方图,但我得到 ValueError: bins must increase monotonically. 原始图像来自 here,这里是我的代码:

from skimage import io, exposure
import matplotlib.pyplot as plt

img = io.imread('img/coins_black_small.jpg', as_grey=True)
hist,bins=exposure.histogram(img)

plt.hist(bins,hist)

ValueError: bins must increase monotonically.

但是当我对 bins 值进行排序时出现同样的错误:

import numpy as np
sorted_bins = np.sort(bins)
plt.hist(sorted_bins,hist)

ValueError: bins must increase monotonically.

我终于尝试检查 bins 值,但在我看来它们似乎是有序的(对于此类测试的任何建议也将不胜感激):

if any(bins[:-1] >= bins[1:]):
    print "bim"

没有输出。

对发生的事情有什么建议吗?
我正在努力学习Python,所以请宽容。这是我的安装(在 Linux Mint 上):

Matplotlib hist 接受数据作为第一个参数,而不是已经装箱的计数。使用 matplotlib bar 绘制它。请注意,与 numpy histogram 不同,skimage exposure.histogram returns 垃圾箱的中心。

width = bins[1] - bins[0]
plt.bar(bins, hist, align='center', width=width)
plt.show()

plt.hist的签名是plt.hist(data, bins, ...)。因此,您试图将已经计算出的直方图作为 bin 插入到 matplotlib hist 函数中。直方图当然没有排序,因此 "bins must increase monotonically"-error 被抛出。

虽然您当然可以使用 plt.hist(hist, bins),但直方图是否有用还是值得怀疑的。我猜你想简单地绘制第一个直方图的结果。

为此目的使用条形图很有意义:

hist,bins=numpy.histogram(img)
plt.bar(bins[:-1], hist, width=(bins[-1]-bins[-2]), align="edge")

正确的解法是:

All bin values must be whole numbers, no decimals! You can use round() function.