官术网_书友最值得收藏!

Plotting histograms

Histograms are graphical representations of a probability distribution. In fact, a histogram is just a specific kind of a bar chart. We could easily use matplotlib's bar chart function and do some statistics to generate histograms. However, histograms are so useful that matplotlib provides a function just for them. In this recipe, we are going to see how to use this histogram function.

How to do it...

The following script draws 1000 values from a normal distribution and then generates histograms with 20 bins:

import numpy as np
import matplotlib.pyplot as plt

X = np.random.randn(1000)

plt.hist(X, bins = 20)
plt.show()

The histogram will change a bit each time we run the script as the dataset is randomly generated. The preceding script will display the following graph:

How to do it...

How it works...

The pyplot.hist() function takes a list of values as the input. The range of the values will be divided into equal-sized bins (10 bins by default). The pyplot.hist() function will generate a bar chart, one bar for one bin. The height of one bar is the number of values following in the corresponding bin. The number of bins is determined by the optional parameter bins. By setting the optional parameter normed to True, the bar height is normalized and the sum of all bar heights is equal to 1.

主站蜘蛛池模板: 北辰区| 和林格尔县| 普宁市| 乡城县| 合山市| 尼勒克县| 鱼台县| 胶州市| 上饶县| 徐水县| 沈丘县| 农安县| 青龙| 德庆县| 容城县| 丘北县| 阿拉善盟| 德江县| 南皮县| 台前县| 临洮县| 响水县| 榕江县| 西乌珠穆沁旗| 太原市| 翁牛特旗| 肥城市| 兴宁市| 三都| 府谷县| 岚皋县| 富顺县| 平邑县| 周至县| 兰西县| 旺苍县| 洱源县| 措美县| 五常市| 宁陵县| 鸡泽县|