Plot a histogram.
Compute and draw the histogram of x. The return value is a tuple (n, bins, patches) or ([n0, n1, ...], bins, [patches0, patches1, ...]) if the input contains multiple data. See the documentation of the weights parameter to draw a histogram of already-binned data.
Multiple data can be provided via x as a list of datasets of potentially different length ([x0, x1, ...]), or as a 2D ndarray in which each column is a dataset. Note that the ndarray form is transposed relative to the list form.
Masked arrays are not supported.
The bins, range, weights, and density parameters behave as in numpy.histogram
.
Input values, this takes either a single array or a sequence of arrays which are not required to be of the same length.
rcParams["hist.bins"]
(default: 10
)
If bins is an integer, it defines the number of equal-width bins in the range.
If bins is a sequence, it defines the bin edges, including the left edge of the first bin and the right edge of the last bin; in this case, bins may be unequally spaced. All but the last (righthand-most) bin is half-open. In other words, if bins is:
[1, 2, 3, 4]
then the first bin is [1, 2)
(including 1, but excluding 2) and the second [2, 3)
. The last bin, however, is [3, 4]
, which includes 4.
If bins is a string, it is one of the binning strategies supported by numpy.histogram_bin_edges
: 'auto', 'fd', 'doane', 'scott', 'stone', 'rice', 'sturges', or 'sqrt'.
The lower and upper range of the bins. Lower and upper outliers are ignored. If not provided, range is (x.min(), x.max())
. Range has no effect if bins is a sequence.
If bins is a sequence or range is specified, autoscaling is based on the specified bin range instead of the range of x.
If True
, draw and return a probability density: each bin will display the bin's raw count divided by the total number of counts and the bin width (density = counts / (sum(counts) * np.diff(bins))
), so that the area under the histogram integrates to 1 (np.sum(density * np.diff(bins)) == 1
).
If stacked is also True
, the sum of the histograms is normalized to 1.
An array of weights, of the same shape as x. Each value in x only contributes its associated weight towards the bin count (instead of 1). If density is True
, the weights are normalized, so that the integral of the density over the range remains 1.
This parameter can be used to draw a histogram of data that has already been binned, e.g. using numpy.histogram
(by treating each bin as a single point with a weight equal to its count)
counts, bins = np.histogram(data) plt.hist(bins[:-1], bins, weights=counts)
(or you may alternatively use bar()
).
If True
, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values. The last bin gives the total number of datapoints.
If density is also True
then the histogram is normalized such that the last bin equals 1.
If cumulative is a number less than 0 (e.g., -1), the direction of accumulation is reversed. In this case, if density is also True
, then the histogram is normalized such that the first bin equals 1.
Location of the bottom of each bin, ie. bins are drawn from bottom
to bottom + hist(x, bins)
If a scalar, the bottom of each bin is shifted by the same amount. If an array, each bin is shifted independently and the length of bottom must match the number of bins. If None, defaults to 0.
The type of histogram to draw.
The horizontal alignment of the histogram bars.
If 'horizontal', barh
will be used for bar-type histograms and the bottom kwarg will be the left edges.
The relative width of the bars as a fraction of the bin width. If None
, automatically compute the width.
Ignored if histtype is 'step' or 'stepfilled'.
If True
, the histogram axis will be set to a log scale.
Color or sequence of colors, one per dataset. Default (None
) uses the standard line color sequence.
String, or sequence of strings to match multiple datasets. Bar charts yield multiple patches per dataset, but only the first gets the label, so that legend
will work as expected.
If True
, multiple data are stacked on top of each other If False
multiple data are arranged side by side if histtype is 'bar' or on top of each other if histtype is 'step'
The values of the histogram bins. See density and weights for a description of the possible semantics. If input x is an array, then this is an array of length nbins. If input is a sequence of arrays [data1, data2, ...]
, then this is a list of arrays with the values of the histograms for each of the arrays in the same order. The dtype of the array n (or of its element arrays) will always be float even if no weighting or normalization is used.
The edges of the bins. Length nbins + 1 (nbins left edges and right edge of last bin). Always a single array even when multiple data sets are passed in.
BarContainer
or list of a single Polygon
or list of such objects
Container of individual artists used to create the histogram or list of such containers if there are multiple input datasets.
If given, the following parameters also accept a string s
, which is interpreted as data[s]
(unless this raises an exception):
x, weights
Patch
properties
For large numbers of bins (>1000), 'step' and 'stepfilled' can be significantly faster than 'bar' and 'barstacked'.
matplotlib.axes.Axes.hist
© 2012–2021 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.axes.Axes.hist.html