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

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.

主站蜘蛛池模板: 瑞安市| 鹤庆县| 盐源县| 竹山县| 金坛市| 天峻县| 蕲春县| 汤原县| 临夏县| 南靖县| 山丹县| 牡丹江市| 阿克陶县| 吉水县| 内黄县| 甘孜| 克什克腾旗| 辽宁省| 克东县| 仁化县| 孝感市| 东海县| 灵山县| 克拉玛依市| 清涧县| 吉木萨尔县| 阳高县| 喀喇沁旗| 黄石市| 湾仔区| 鹤壁市| 桐梓县| 安多县| 微博| 明光市| 长岭县| 汽车| 武夷山市| 太仓市| 琼海市| 南溪县|