- matplotlib Plotting Cookbook
- Alexandre Devert
- 220字
- 2021-07-16 12:16:26
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 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.
- Mastering Visual Studio 2017
- Learning C# by Developing Games with Unity 2020
- FreeSWITCH 1.6 Cookbook
- Learning Elixir
- 自然語言處理Python進(jìn)階
- SQL Server數(shù)據(jù)庫管理與開發(fā)兵書
- NGINX Cookbook
- JavaScript+jQuery網(wǎng)頁特效設(shè)計(jì)任務(wù)驅(qū)動(dòng)教程
- Scala Functional Programming Patterns
- 跟戴銘學(xué)iOS編程:理順核心知識(shí)點(diǎn)
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- 算法設(shè)計(jì)與分析:基于C++編程語言的描述
- Mastering JavaScript
- AI自動(dòng)化測(cè)試:技術(shù)原理、平臺(tái)搭建與工程實(shí)踐
- Java EE架構(gòu)設(shè)計(jì)與開發(fā)實(shí)踐