matplotlib.pyplot.hist 的官方文档:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html
这个方法使用 numpy.histogram
首先将 x 中的数据分桶并统计每个桶中的元素数量,接着使用条形图绘制这个分布。
函数参数、含义及样例如下:
from matplotlib import pyplot as plt
import numpy as np
Input values, this takes either a single array or a sequence of arrays which are not required to be of the same length.
plt.hist(np.random.randn(10000))
plt.title("Example 1")
plt.show()
plt.hist([
np.random.randn(10000),
2 * np.random.randn(10000),
3 * np.random.randn(5000)
])
plt.title("Example 2")
plt.show()
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’.
此参数用于传给
numpy.histogram
的bins
形参,详情见numpy.histogram_bin_edges
的文档。
plt.hist(np.random.randn(10000), bins=20)
plt.title("Example 3")
plt.show()
plt.hist(np.random.randn(10000), bins=[-3, -0.8, 0, 0.3, 3])
plt.title("Example 4")
plt.show()
fd
”plt.hist(np.random.randn(10000), bins="fd")
plt.title("Example 5")
plt.show()
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.
此参数用于传给
numpy.histogram
的range
形参,详情见numpy.histogram_bin_edges
的文档。
plt.hist(np.random.randn(10000), range=(0, 1))
plt.title("Example 6")
plt.show()
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.
plt.hist(np.random.randn(10000), density=True)
plt.title("Example 7")
plt.show()
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.
x = np.random.randn(10000)
weights = 10 * (x >= 0) + 1 * (x < 0)
plt.hist(x, weights=weights)
plt.title("Example 8")
plt.show()
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.
plt.hist(np.random.randn(10000), cumulative=True)
plt.title("Example 9")
plt.show()
Location of the bottom of each bin, i.e. 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.
plt.hist(np.random.randn(10000), bottom=-1000)
plt.title("Example 10")
plt.show()
The type of histogram to draw.
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
histtype="barstacked")
plt.title("Example 11")
plt.show()
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
histtype="step")
plt.title("Example 12")
plt.show()
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
histtype="stepfilled")
plt.title("Example 13")
plt.show()
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.
plt.hist(np.random.randn(10000), orientation="horizontal")
plt.title("Example 14")
plt.show()
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’.
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
stacked=True)
plt.title("Example 15")
plt.show()
>>> n, bins, patches = plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)])
>>> n
[[0.000e+00 0.000e+00 0.000e+00 6.900e+01 3.325e+03 6.092e+03 5.140e+02
0.000e+00 0.000e+00 0.000e+00]
[0.000e+00 2.000e+00 9.700e+01 9.420e+02 3.086e+03 3.797e+03 1.773e+03
2.830e+02 2.000e+01 0.000e+00]
[1.000e+01 5.500e+01 2.200e+02 7.170e+02 1.188e+03 1.341e+03 9.130e+02
4.200e+02 1.130e+02 2.300e+01]]
>>> bins
[-10.7843734 -8.71395108 -6.64352876 -4.57310643 -2.50268411
-0.43226179 1.63816053 3.70858286 5.77900518 7.8494275
9.91984982]
>>> patches
<a list of 3 BarContainer objects>
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
对象的列表BarContainer
or list of a single Polygon
or list of such objectsContainer of individual artists used to create the histogram or list of such containers if there are multiple input datasets.