Open In App

Plotting Histogram in Python using Matplotlib

Last Updated : 13 Jan, 2026
Comments
Improve
Suggest changes
33 Likes
Like
Report

Histograms are one of the most fundamental tools in data visualization. They provide a graphical representation of data distribution, showing how frequently each value or range of values occurs. Histograms are especially useful for analyzing continuous numerical data, such as measurements, sensor readings or experimental results.

A histogram is a type of bar plot where:

  • The X-axis represents intervals (called bins) of the data.
  • The Y-axis represents the frequency of values within each bin.

Unlike regular bar plots, histograms group data into bins to summarize data distribution effectively.

Creating a Matplotlib Histogram

  1. Divide the data range into consecutive, non-overlapping intervals called bins.
  2. Count how many values fall into each bin.
  3. Use the matplotlib.pyplot.hist() function to plot the histogram.

The following table shows the parameters accepted by matplotlib.pyplot.hist() function : 

AttributeParameter
xAn array or sequence of numerical data.
binsNumber of bins (int) or specific intervals (array).
densityIf True, normalises the histogram to show probability instead of frequency.
rangeA tuple specifying lower and upper limits of bins.
histtypeType of histogram: bar, barstacked, step, stepfilled. Default: bar.
alignBin alignment: left, right, mid.
weightsAn array of weights for each data point.
bottomBaseline for bins.
rwidthRelative width of bars (0–1).
colorColor of bars. Can be a single color or sequence.
labelLabel for legend.
logIf True, uses logarithmic scale on Y-axis.

Plotting Histogram in Python using Matplotlib

Here we will see different methods of Plotting Histogram in Matplotlib in Python:

  1. Basic Histogram
  2. Customized Histogram with Density Plot
  3. Customized Histogram with Watermark
  4. Multiple Histograms with Subplots
  5. Stacked Histogram
  6. 2D Histogram (Hexbin Plot)

1. Basic Histogram

Python
import matplotlib.pyplot as plt
import numpy as np

# Generate random data for the histogram
data = np.random.randn(1000)

# Plotting a basic histogram
plt.hist(data, bins=30, color='skyblue', edgecolor='black')

# Adding labels and title
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.title('Basic Histogram')

# Display the plot
plt.show()

Output: