To construct a histogram, the first step is to "bin" (or "bucket") the range of values—that is, divide the entire range of values into a series of intervals.
More information here: https://en.wikipedia.org/wiki/Histogram
Thus if you choose bins equal to 50 then your input will be divided into 50 intervals or bins if possible.
In matplotlib you can also let it automatically generate bins in the latest version given your data or you can give custom sequences as well.
More specific information about matplotlib for bins can be found here: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html
Answer from Udayan Tandon on Stack OverflowWhat is Python binning?
What are the benefits of binning in Python?
What are the different techniques for binning data in Python?
It's probably faster and easier to use numpy.digitize():
import numpy
data = numpy.random.random(100)
bins = numpy.linspace(0, 1, 10)
digitized = numpy.digitize(data, bins)
bin_means = [data[digitized == i].mean() for i in range(1, len(bins))]
An alternative to this is to use numpy.histogram():
bin_means = (numpy.histogram(data, bins, weights=data)[0] /
numpy.histogram(data, bins)[0])
Try for yourself which one is faster... :)
The Scipy (>=0.11) function scipy.stats.binned_statistic specifically addresses the above question.
For the same example as in the previous answers, the Scipy solution would be
import numpy as np
from scipy.stats import binned_statistic
data = np.random.rand(100)
bin_means = binned_statistic(data, data, bins=10, range=(0, 1))[0]
The bins parameter tells you the number of bins that your data will be divided into. You can specify it as an integer or as a list of bin edges.
For example, here we ask for 20 bins:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1000)
plt.hist(x, bins=20)

And here we ask for bin edges at the locations [-4, -3, -2... 3, 4].
plt.hist(x, bins=range(-4, 5))

Your question about how to choose the "best" number of bins is an interesting one, and there's actually a fairly vast literature on the subject. There are some commonly-used rules-of-thumb that have been proposed (e.g. the Freedman-Diaconis Rule, Sturges' Rule, Scott's Rule, the Square-root rule, etc.) each of which has its own strengths and weaknesses.
If you want a nice Python implementation of a variety of these auto-tuning histogram rules, you might check out the histogram functionality in the latest version of the AstroPy package, described here.
This works just like plt.hist, but lets you use syntax like, e.g. hist(x, bins='freedman') for choosing bins via the Freedman-Diaconis rule mentioned above.
My personal favorite is "Bayesian Blocks" (bins="blocks"), which solves for optimal binning with unequal bin widths. You can read a bit more on that here.
Edit, April 2017: with matplotlib version 2.0 or later and numpy version 1.11 or later, you can now specify automatically-determined bins directly in matplotlib, by specifying, e.g. bins='auto'. This uses the maximum of the Sturges and Freedman-Diaconis bin choice. You can read more about the options in the numpy.histogram docs.
To complemented jakes answer, you can use
numpy.histogram_bin_edges if you just want to calculate the optimal bin edges, without actually doing the histogram. histogram_bin_edges is a function specifically designed for the optimal calculation of bin edges. You can choose seven different algorithms for the optimisation.