垃圾箱必须单调增加

2024-04-20 08:01:45 发布

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

我只想从skiplage.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上):

  • Python 2.7.13::Anaconda 4.3.1(64位)
  • 朱庇特4.2.1

Tags: ioimportimg排序asnpplthist
2条回答

Matplotlibhist接受数据作为第一个参数,而不是已装箱的计数。使用matplotlibbar来绘制它。注意,与numpy histogram不同,skipage exposure.histogram返回容器的中心。

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

plt.hist的签名是plt.hist(data, bins, ...)。因此,您正试图将已计算的直方图作为bin插入matplotlibhist函数。直方图当然没有排序,因此“存储箱必须单调增加”-错误被抛出。

当然,你可以使用plt.hist(hist, bins),但是直方图的组织编程是否有用是个问题。我猜你只是想简单地描绘出第一次历史编程的结果。

为此,使用条形图是有意义的:

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

相关问题 更多 >