bins must increase monotonically

Aurélien Vasseur picture Aurélien Vasseur · May 24, 2017 · Viewed 30.7k times · Source

I just want to draw Matplotlib histograms from skimage.exposure but I get a ValueError: bins must increase monotonically. The original image comes from here and here my code:

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.

But the same error arises when I sort the bins values:

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

ValueError: bins must increase monotonically.

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):

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 2.7.13 :: Anaconda 4.3.1 (64-bit)
  • Jupyter 4.2.1

Answer

Ondro picture Ondro · May 24, 2017

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.

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