Histograms are commonly used to plot data over an interval. In addition, it plots the frequency of values over an interval. It can be plotted by using the hist() function of the matplotlib module.

The syntax for plotting a histogram in Python is:

matplotlib.pyplot.hist(xbins=Nonerange=Nonedensity=Noneweights=Nonecumulative=Falsebottom=Nonehisttype='bar'align='mid'orientation='vertical'rwidth=Nonelog=Falsecolor=Nonelabel=Nonestacked=Falsenormed=None*data=None**kwargs)[source]

A simple example of a histogram in Python is given below.

import matplotlib.pyplot as plt
import numpy as np

# Setting the figure size
plt.figure(figsize=(10, 8))

# Randomizing values of x
x = np.random.normal(size = 100)

# Plotting the histogram
plt.hist(x, normed=True, bins=10)

# Setting labels
plt.title("Sample Histogram Plot")
plt.xlabel('x-axis')
plt.ylabel('y-axis')

# Display the plot
plt.show()
Histogram in Python

In Conclusion

Histograms are popularly used for visualization of data at different intervals in Python. You may like this tutorial on other kinds of plots in Python: Data Visualization in Python (With Tutorials).

LEAVE A REPLY

Please enter your comment!
Please enter your name here