لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science Matplotlib Histograms

A histogram is a graph showing frequency distributions. It is a graph which shows the number of observations within each given interval.

Example 1: Say you ask for the height of 500 people. For simplicity we use NumPy to randomly generate an array with 500 values, where the values will concentrate around 200, and the standard deviation is 20. A Normal Data Distribution by NumPy for the same would be as below

Code

import numpy as np
x = np.random.normal(200, 20, 500)
print(x)

output will be.

[195.36333694 192.06460493 218.93509457 195.24248188 234.66413888

166.74307532 210.40697405 192.56005625 210.55142102 198.83900029

191.97539521 196.01586341 200.76734128 212.52039468 174.44675673

180.90527027 153.81702515 219.56135173 212.68870626 225.92293157

208.58466086 194.28241739 190.65118445 203.61801532 209.11576321

225.92169072 206.48330702 174.39563859 204.34055471 218.5191373

189.44132995 247.36600584 211.22822072 183.84328057 188.2334376

185.75479718 204.77807488 196.3768934 233.22471424 197.27819998

193.13740968 197.63352381 161.07808481 203.00963914 176.29631661

179.96834999 181.35536284 225.22458838 184.76936163 225.18584262

166.99884947 210.43240855 204.88361048 178.88200294 187.4227057

199.88540048 192.85355153 162.35992718 188.14772211 201.68907642

223.52214212 191.99727571 202.35457702 188.57703486 219.99525613

182.10572405 221.71640978 207.8013061 205.17327442 197.07047242

192.6506295 215.33982181 190.26649268 230.48862539 207.52473986

211.85026947 213.14747757 172.05945107 200.04811729 206.12290732

220.27703016 193.79485011 204.65265608 227.46063922 214.65841827

219.8280764 216.19849487 199.88581886 174.67888692 219.53496357

182.80292395 199.29436187 182.49034489 196.45664876 226.31222833

224.64141224 217.16782924 206.9297814 187.9502181 184.16428227

199.55479694 181.45708225 203.44445732 173.56684285 194.73565197

182.08618319 159.17264196 219.17680412 207.74895885 177.51830868

167.36812446 232.71630018 198.94117831 194.55559781 219.9527305

188.2047885 220.13356911 205.57753774 224.97596912 204.27549725

226.54088355 220.03986751 209.12991298 193.28625794 195.12288216

161.18452988 181.76059946 171.80355112 177.15226789 182.99650714

218.65869153 202.17181863 180.15185001 217.8130761 223.37278845

167.17197676 200.83825569 212.64130445 201.22620911 179.65955848

231.60335311 231.62496372 185.74067282 203.43785436 201.49138313

204.35165361 197.9811102 200.43635228 178.04099814 188.43067772

192.84815743 170.10356247 200.1792692 209.60089646 183.20954713

221.41775735 196.54471293 201.18773076 226.17212868 201.85421682

226.46057346 181.87117198 175.43403886 209.84965406 195.90385023

215.37039043 182.38714078 221.44471725 166.22923627 175.98797322

178.64314409 201.0449044 202.91080377 207.40519316 252.68797533

174.61856807 170.77506812 217.03605868 193.13526611 166.26358532

190.13682704 200.79534915 165.66339789 217.90841882 225.36975642

222.45259502 201.07610584 177.73008085 222.75207893 222.18360875

231.50565138 198.94214301 213.42982242 167.89435235 170.68593088

158.19619818 173.60421078 157.26148128 181.06366458 204.41541034

197.8136497 169.31513633 234.8853614 206.82638553 210.99921825

221.48861329 204.13073283 208.27869572 201.32000559 189.71129667

191.19372563 143.25688724 203.38253169 191.08007489 199.92232023

211.8384695 202.62412642 201.05613373 224.30206751 213.62042235

219.60930627 170.12307196 238.67769243 207.03991678 176.55347511

190.28882751 205.47045517 213.88031133 223.03674095 217.69748819

222.63391925 213.42666114 225.70051173 229.12129836 209.46335361

202.32238978 201.53795974 187.29455279 182.81579434 181.00730496

207.8268121 189.51358775 180.18701511 197.86827999 205.41349265

188.18187242 194.87862921 208.29845944 203.21245917 178.33349474

223.63715935 201.39800409 221.50216819 235.27971803 174.21789166

204.1070133 200.81644489 207.87995505 205.73600773 184.030866

188.46254315 170.83650969 238.46271228 201.86010815 188.40186723

197.86720026 208.15616004 239.07311987 232.32215789 178.22587543

192.38949 180.70030588 184.16878883 206.2938351 233.65932158

232.41800745 192.32119649 202.17390133 163.06832224 218.48022566

209.54980892 205.40638554 223.92154275 184.45694064 202.00417447

160.49696128 241.92914004 184.61739257 205.91683027 171.92292526

190.0605478 209.69798899 185.28345155 188.44177591 218.56122736

220.11184376 154.86826218 193.44177009 216.10107172 211.16476197

206.18237021 181.84254946 145.38357021 194.73201781 182.13090587

217.89572334 208.50983206 204.76684887 172.66917469 214.96984041

195.14846964 214.27567531 187.40663759 209.60892202 214.39923467

210.50473887 220.28643214 193.06923532 177.40686966 141.27156467

160.63656038 192.6694537 182.52872469 240.56981992 198.27920068

201.8744315 185.70593784 183.68867038 200.88810269 190.78387773

205.3011619 201.91069007 189.58688499 190.88758037 189.49100731

204.16449307 182.58766516 178.74198042 191.43423478 235.91065252

211.27962782 200.50071416 191.00757341 210.85322405 203.75393279

204.60095848 205.56265679 174.87790969 184.96423371 200.41727973

172.13156736 209.13949709 198.38019369 171.84042879 161.62441819

216.04218623 213.06571576 194.56759687 189.35477189 193.90761867

204.30375127 215.53253825 218.49891903 178.60974867 212.83953441

195.9253538 197.78332563 200.85387868 214.72173327 217.87563626

194.6629066 215.92259984 172.49538669 204.75844164 176.34147657

200.07988204 180.640734 207.22541623 219.43971639 198.06229583

213.41387101 186.08064781 219.89241243 204.79421953 185.32832757

210.94637639 160.29422175 208.36319911 180.37095782 186.55408942

233.82527422 194.23478718 199.47422712 211.62809086 139.11379776

244.99322769 195.53008332 181.02924774 226.45734929 187.37265524

178.15615405 210.30232452 203.32341776 196.68181384 181.90427673

167.16579275 213.89407641 157.77653731 219.65809284 222.90507485

188.97890169 211.19261123 195.88970739 203.05807289 213.69609999

185.32321449 150.36244301 204.16265456 208.63164506 201.68417265

187.67268452 205.01578644 210.57348595 189.10261937 166.44047946

190.53684622 173.55985938 209.27485549 189.84116645 217.38315582

209.34282111 197.85069049 200.50165725 172.41284362 220.67753905

166.67550193 206.36777504 171.39835889 187.84622853 204.76374258

230.27547922 212.09386118 221.8447369 167.77733768 210.78185061

167.86055163 187.56715054 230.3492913 182.79732976 206.54524613

196.32301393 171.38664311 185.62405555 198.57999365 204.65383958

171.65151634 183.94323892 205.69072279 180.48619703 206.08705353

167.71737247 171.07731175 148.36017927 171.05381141 198.40091528

211.24051935 192.18876963 184.85924203 163.94380993 200.91168103

220.39829333 194.54255413 213.76845251 199.48613248 172.89349755

173.7570742 202.38295923 223.12509124 194.60600231 212.64478182

207.34592652 206.48460655 194.77276339 210.26637351 191.82114395

179.95273952 202.69885156 196.51698092 194.53466175 213.80327263

188.44926481 186.77014874 179.22314327 195.35250765 182.73953743

234.11567115 173.29825488 179.30829532 200.989412 172.85687698]

Now in order to generate a histogram for the above data.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(200, 20, 500)

plt.hist(x)
plt.show()

the output will be.


Observations: You can read from the histogram that there are approximately:


2 people from 140 to 150cm
10 people from 150 to 160cm
40 people from 160 to 170cm
68 people from 170 to 180cm
118 people from 180 to 190cm
102 people from 200 to 210cm
94 people from 210 to 220cm
38 people from 220 to 230cm
15 people from 230 to 240cm
11 people from 240 to 250cm
2 people from 250 to 260cm