# Matplotlib Histogram – Python Matplotlib Tutorial

## Python Matplotlib Histogram

Matplotlib histogram is a representation of numeric data in the form of a rectangle bar. Each bar shows some data,  which belong to different categories. To plot histogram using python matplotlib library need plt.hist() method.

Syntax: plt.hist(
x,
bins=None,
range=None,
density=None,
weights=None,
cumulative=False,
bottom=None,
histtype=’bar’,
align=’mid’,
orientation=’vertical’,
rwidth=None,
log=False,
color=None,
label=None,
stacked=False,
normed=None,
*,
data=None,
**kwargs,
)

The plt.hist() method has lots of parameter, So we are going to cover some of these. Which is most important but you have to try each and every one to best practice.

#### Importing Packages

```import matplotlib.pyplot as plt
import numpy as np
import random
```

#### Generate Data

```ml_students_age = np.random.randint(18,45, (100))
py_students_age = np.random.randint(15,40, (100))
```

#### Printing Generated Data

```print(ml_students_age)
print(py_students_age)
```
```[22 20 22 42 40 21 35 23 22 24 24 21 44 25 24 40 41 44 44 44 19 23 33 27
29 21 28 18 34 31 32 29 32 18 39 23 26 24 33 21 20 26 38 25 38 31 30 20
39 32 41 20 24 27 26 22 33 31 22 38 37 33 34 28 28 34 34 34 33 40 34 23
33 39 39 25 42 27 23 28 33 31 44 39 28 26 25 29 23 39 39 38 41 34 26 38
35 42 31 29]
[21 29 26 22 21 20 29 29 26 38 28 32 35 20 21 16 39 26 39 31 27 23 29 37
32 30 21 36 18 32 17 20 18 28 17 30 29 26 35 31 19 19 19 39 21 26 27 17
23 22 37 21 35 37 16 33 36 39 31 33 37 26 26 17 17 17 23 27 28 32 38 20
19 33 24 36 34 27 25 21 33 15 39 15 37 27 32 35 21 37 16 38 36 18 39 21
29 27 18 30]
```

#### Plotting Histogram Using Matplotlib

Plotting histogram of machine learning students age.

```plt.hist(ml_students_age)

plt.title("ML Students age histograms")
plt.xlabel("Students age cotegory")
plt.ylabel("No. Students age")
plt.show()
```

Output >>>

#### Plotting Histogram Using Matplotlib with parameters

```bins = [15,20,25,30,35,40,45] # category of ML students age on x axis
plt.figure(figsize = (16,9)) # size of histogram in 16:9 format

plt.hist(ml_students_age, bins, rwidth=0.8, histtype = "bar",
orientation='vertical', color = "m", label = "ML Student")

plt.title("ML Students age histograms")
plt.xlabel("Students age cotegory")
plt.ylabel("No. Students age")
plt.legend()
plt.show()
```

Output >>>

#### Plotting two Histogram Using Matplotlib with parameters

```from matplotlib import style # for style
style.use("ggplot") # return grid
plt.figure(figsize = (16,9))

plt.hist([ml_students_age, py_students_age], bins, rwidth=0.8, histtype = "bar",
orientation='vertical', color = ["m", "y"], label = ["ML Student", "Py Student"])

#plt.hist(py_students_age, bins, rwidth=0.8, histtype = "bar",
#         orientation='vertical', color = "y", label = "Py Student")

plt.title("ML &amp; Py Students age histograms")
plt.xlabel("Students age cotegory")
plt.ylabel("No. Students age")
plt.legend()
plt.show()
```

Output >>>

### Conclusion

In matplotlib histogram blog, we learn how to plot one and multiple histograms with a real-time example using plt.hist() method. Along with that used different method with different parameter. We suggest you make your hand dirty with each and every parameter of the above methods. This is the best coding practice. After completion of the matplotlib tutorial jump on Seaborn.