[Matplotlib-users] axes.hist() with 2D input

Paul Hobson pmhobson at gmail.com
Thu Oct 6 16:48:29 EDT 2016

pyplot.hist(x) where x is a N (rows) x M (cols) array will generate M
histograms and plot on a single Axes object.

Consider an array that likely has far fewer columns than your image:

import numpy
from matplotlib import pyplot

data = numpy.random.normal(size=(37, 4))
fig, ax = pyplot.subplots()

Does that make sense?

On Thu, Oct 6, 2016 at 5:36 AM, Poughon Victor <Victor.Poughon at cnes.fr>

> Hi,
> Working with images as 2D numpy arrays, it's very confusing to find that:
> plt.hist(image.flatten())
> plt.hist(image)
> produce such different histograms. What I am expecting is the first one,
> which is the histogram of the image. But if I forget to flatten the array
> (which I don't need to do if I use numpy.histogram and then plot, adding to
> the confusion), the plotted histogram is strange. The number of bins is
> different, and so are the frequencies.
> I'm not sure what the point of this message is, but I'd like to share my
> experience with this. I just spend a good 30 minutes trying to understand
> why the matplotlib histogram of my image was clearly wrong. The pyplot.hist
> and axes.hist documentation are not so great in explaining what is plotted
> with 2D input (I'm still not sure what I'm looking at), or at building an
> expectation that if you are working with an image, plt.hist(image) is NOT
> what you want.
> To sum up, numpy.histogram and matplotlib's hist() have VERY different
> behaviors for 2D input arrays. numpy flattens, and matplotlib does... what
> exactly?
> Any thoughts?
> cheers,
> Victor Poughon
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20161006/29647eae/attachment.html>

More information about the Matplotlib-users mailing list