垃圾箱必须单调增加

Aur*_*eur 7 python matplotlib scikit-image

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

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)
Run Code Online (Sandbox Code Playgroud)

ValueError:bin必须单调增加.

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

import numpy as np
sorted_bins = np.sort(bins)
plt.hist(sorted_bins,hist)
Run Code Online (Sandbox Code Playgroud)

ValueError:bin必须单调增加.

我终于尝试检查了箱子的值,但是我认为它们似乎是排序的(对于这种测试的任何建议也会受到赞赏):

if any(bins[:-1] >= bins[1:]):
    print "bim"
Run Code Online (Sandbox Code Playgroud)

没有输出.

关于会发生什么的任何建议?
我正在努力学习Python,所以请放纵.这是我的安装(在Linux Mint上):

  • Python 2.7.13 :: Anaconda 4.3.1(64位)
  • Jupyter 4.2.1

Ond*_*dro 9

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

width = bins[1] - bins[0]
plt.bar(bins, hist, align='center', width=width)
plt.show()
Run Code Online (Sandbox Code Playgroud)


小智 5

正确的解决方案是:

所有 bin 值必须是整数,没有小数!您可以使用 round() 函数。