Bunkers must increase monotonically
I just want to plot Matplotlib histograms from skimage.exposure, but I am getting ValueError: bins must increase monotonically.
Original image originated from here and here's 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 occurs when I sort the bean values:
import numpy as np
sorted_bins = np.sort(bins)
plt.hist(sorted_bins,hist)
ValueError: Bins must increase monotonically.
Finally, I tried to check the values โโof the bins, but they seem to be sorted in my opinion (any advice for such a test would be appreciated as well):
if any(bins[:-1] >= bins[1:]):
print "bim"
There is no conclusion from this.
Any suggestion on what's going on?
I'm trying to learn Python, so be lenient please. Here's my setup (on Linux Mint):
- Python 2.7.13 :: Anaconda 4.3.1 (64-bit)
- Jupyter 4.2.1
source to share
Matplotlib hist
takes data as the first argument, not a counted counter. Use matplotlib bar
to plot it. Note that unlike numpy histogram
, skimage exposure.histogram
returns the centers of the bins.
width = bins[1] - bins[0] plt.bar(bins, hist, align='center', width=width) plt.show()
source to share
The signature plt.hist
is equal plt.hist(data, bins, ...)
. So you are trying to plug an already computed histogram as bins into the matplotlib function hist
. The histogram is, of course, not sorted, and therefore "the bins must increase monotonically" is the challenge.
While you can of course use it plt.hist(hist, bins)
, it is doubtful if the histogram of the histogram would be used. I would suggest that you just want to plot the result of the first histogram.
Using a histogram would make sense for this purpose:
hist,bins=numpy.histogram(img)
plt.bar(bins[:-1], hist, width=(bins[-1]-bins[-2]), align="edge")
source to share