matplotlib.pyplot.hist 的官方文檔:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html
matplotlib.pyplot.hist
這個(gè)方法使用 numpy.histogram
首先將 x 中的數(shù)據(jù)分桶并統(tǒng)計(jì)每個(gè)桶中的元素?cái)?shù)量,接著使用條形圖繪制這個(gè)分布。
函數(shù)參數(shù)、含義及樣例如下:
from matplotlib import pyplot as plt
import numpy as np
參數(shù)列表及樣例
x : 數(shù)據(jù)集對(duì)象(必填)
- (n,) array or sequence of (n,) arrays
Input values, this takes either a single array or a sequence of arrays which are not required to be of the same length.
樣例 1:當(dāng) x 為多維向量時(shí),繪制多維向量的直方圖
plt.hist(np.random.randn(10000))
plt.title("Example 1")
plt.show()
樣例 2:當(dāng) x 為 m 個(gè)多維向量的列表時(shí)(各個(gè)向量的長(zhǎng)度不需要全部相同),將在同一個(gè)軸上分別繪制 m 個(gè)多維向量的直方圖
plt.hist([
np.random.randn(10000),
2 * np.random.randn(10000),
3 * np.random.randn(5000)
])
plt.title("Example 2")
plt.show()
bins : 分桶數(shù) / 分桶數(shù)值(非必填)
- int or sequence or str, default:
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
: ‘a(chǎn)uto’, ‘fd’, ‘doane’, ‘scott’, ‘stone’, ‘rice’, ‘sturges’, or ‘sqrt’.
此參數(shù)用于傳給
numpy.histogram
的bins
形參,詳情見(jiàn)numpy.histogram_bin_edges
的文檔。
樣例 3:當(dāng) bins 傳入整數(shù)時(shí),會(huì)分為 bins 數(shù)量的等寬桶
plt.hist(np.random.randn(10000), bins=20)
plt.title("Example 3")
plt.show()
樣例 4:當(dāng) bins 傳入序列時(shí),會(huì)視作每個(gè)桶的邊界
plt.hist(np.random.randn(10000), bins=[-3, -0.8, 0, 0.3, 3])
plt.title("Example 4")
plt.show()
樣例 5:當(dāng)傳入字符串時(shí),例如 “fd
”
plt.hist(np.random.randn(10000), bins="fd")
plt.title("Example 5")
plt.show()
range : 桶的下界和上界(非必填)
- tuple or None, default: None
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.
此參數(shù)用于傳給
numpy.histogram
的range
形參,詳情見(jiàn)numpy.histogram_bin_edges
的文檔。
樣例 6:使用 range 參數(shù)
plt.hist(np.random.randn(10000), range=(0, 1))
plt.title("Example 6")
plt.show()
density : y 軸是否使用頻率(非必填)
- bool, default: False
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.
樣例 7:顯示頻率
plt.hist(np.random.randn(10000), density=True)
plt.title("Example 7")
plt.show()
weight :是否為元素賦予權(quán)重
- (n,) array-like or None, default: None
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.
樣例 8:配置權(quán)重
x = np.random.randn(10000)
weights = 10 * (x >= 0) + 1 * (x < 0)
plt.hist(x, weights=weights)
plt.title("Example 8")
plt.show()
cumulative : 是否繪制累加直方圖
- bool or -1, default: False
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.
樣例 9:累加直方圖
plt.hist(np.random.randn(10000), cumulative=True)
plt.title("Example 9")
plt.show()
bottom : 每個(gè)桶的初始值
- array-like, scalar, or None, default: None
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.
樣例 10:統(tǒng)一的桶初始值
plt.hist(np.random.randn(10000), bottom=-1000)
plt.title("Example 10")
plt.show()
histype : 多個(gè)數(shù)據(jù)的直方圖類(lèi)型
- {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}, default: ‘bar’
The type of histogram to draw.
- ‘bar’ is a traditional bar-type histogram. If multiple data are given the bars are arranged side by side.
- ‘barstacked’ is a bar-type histogram where multiple data are stacked on top of each other.
- ‘step’ generates a lineplot that is by default unfilled.
- ‘stepfilled’ generates a lineplot that is by default filled.
樣例 11:barstacked 類(lèi)型直方圖
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
histtype="barstacked")
plt.title("Example 11")
plt.show()
樣例 12:step 類(lèi)型直方圖
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
histtype="step")
plt.title("Example 12")
plt.show()
樣例 13:stepfilled 類(lèi)型直方圖
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
histtype="stepfilled")
plt.title("Example 13")
plt.show()
align : 對(duì)齊方式
- {‘left’, ‘mid’, ‘right’}, default: ‘mid’
The horizontal alignment of the histogram bars.
- ‘left’: bars are centered on the left bin edges.
- ‘mid’: bars are centered between the bin edges.
- ‘right’: bars are centered on the right bin edges.
orientation : 橫向 / 縱向柱狀圖
- {‘vertical’, ‘horizontal’}, default: ‘vertical’
If ‘horizontal’, barh
will be used for bar-type histograms and the bottom kwarg will be the left edges.
樣例 14:橫向直方圖
plt.hist(np.random.randn(10000), orientation="horizontal")
plt.title("Example 14")
plt.show()
rwidth : 條形圖的相對(duì)寬度
- float or None, default: None
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’.
log : 是否對(duì) y 軸使用對(duì)數(shù)縮放
- bool, default: False
If True
, the histogram axis will be set to a log scale.
color : 桶的顏色
- color or array-like of colors or None, default: None
Color or sequence of colors, one per dataset. Default (None
) uses the standard line color sequence.
label : 標(biāo)簽
- str or None, default: None
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.
stacked : 是否繪制堆積圖
- bool, default: False
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’.
樣例 15:繪制堆積圖
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>
n : 每一個(gè)堆的樣本數(shù)
- array or list of arrays
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.
bins : 堆的邊界
- array
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.文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-733812.html
patches : BarContainer
對(duì)象的列表
-
BarContainer
or list of a singlePolygon
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.文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-733812.html
到了這里,關(guān)于Python 實(shí)例|matplotlib|繪制直方圖(各參數(shù)樣例)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!