Python bins 必须单调增加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44166763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bins must increase monotonically
提问by Aurélien Vasseur
I just want to draw Matplotlib histograms from skimage.exposure but I get a ValueError: bins must increase monotonically.
The original image comes from hereand here my code:
我只想从 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)
ValueError: bins must increase monotonically.
ValueError: bins 必须单调增加。
But the same error arises when I sort the bins values:
但是当我对 bin 值进行排序时会出现同样的错误:
import numpy as np
sorted_bins = np.sort(bins)
plt.hist(sorted_bins,hist)
ValueError: bins must increase monotonically.
ValueError: bins 必须单调增加。
I finally tried to check the bins values, but they seem sorted in my opinion (any advice for this kind of test would appreciated also):
我终于尝试检查 bins 值,但我认为它们似乎已排序(对于此类测试的任何建议也将不胜感激):
if any(bins[:-1] >= bins[1:]):
print "bim"
No output from this.
没有输出。
Any suggestion on what happens?
I'm trying to learn Python, so be indulgent please. Here my install (on Linux Mint):
关于会发生什么的任何建议?
我正在尝试学习 Python,所以请放纵一下。这是我的安装(在 Linux Mint 上):
- Python 2.7.13 :: Anaconda 4.3.1 (64-bit)
- Jupyter 4.2.1
- Python 2.7.13 :: Anaconda 4.3.1(64 位)
- Jupyter 4.2.1
回答by Ondro
Matplotlib hist
accept data as first argument, not already binned counts. Use matplotlib bar
to plot it. Note that unlike numpy histogram
, skimage exposure.histogram
returns the centers of bins.
Matplotlibhist
接受数据作为第一个参数,而不是已经分箱的计数。使用 matplotlibbar
绘制它。请注意,与 numpy 不同histogram
, skimageexposure.histogram
返回 bin 的中心。
width = bins[1] - bins[0]
plt.bar(bins, hist, align='center', width=width)
plt.show()
回答by ImportanceOfBeingErnest
The signature of plt.hist
is plt.hist(data, bins, ...)
. So you are trying to plug the already computed histogram as bins into the matplotlib hist
function. The histogram is of course not sorted and therefore the "bins must increase monotonically"-error is thrown.
的签名plt.hist
是plt.hist(data, bins, ...)
。所以你试图将已经计算的直方图作为 bin 插入到 matplotlibhist
函数中。直方图当然没有排序,因此会抛出“垃圾箱必须单调增加”的错误。
While you could of course use plt.hist(hist, bins)
, it's questionable if histogramming a histogram is of any use. I would guess that you want to simply plot the result of the first histogramming.
虽然您当然可以使用plt.hist(hist, bins)
,但对直方图进行直方图绘制是否有任何用处是值得怀疑的。我猜你想简单地绘制第一个直方图的结果。
Using a bar chart would make sense for this purpose:
为此目的,使用条形图是有意义的:
hist,bins=numpy.histogram(img)
plt.bar(bins[:-1], hist, width=(bins[-1]-bins[-2]), align="edge")