Plotting Histogram in Python using Matplotlib
Last Updated :
13 Jan, 2026
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
- Divide the data range into consecutive, non-overlapping intervals called bins.
- Count how many values fall into each bin.
- Use the matplotlib.pyplot.hist() function to plot the histogram.
The following table shows the parameters accepted by matplotlib.pyplot.hist() function :
| Attribute | Parameter |
|---|
| x | An array or sequence of numerical data. |
|---|
| bins | Number of bins (int) or specific intervals (array). |
|---|
| density | If True, normalises the histogram to show probability instead of frequency. |
|---|
| range | A tuple specifying lower and upper limits of bins. |
|---|
| histtype | Type of histogram: bar, barstacked, step, stepfilled. Default: bar. |
|---|
| align | Bin alignment: left, right, mid. |
|---|
| weights | An array of weights for each data point. |
|---|
| bottom | Baseline for bins. |
|---|
| rwidth | Relative width of bars (0–1). |
|---|
| color | Color of bars. Can be a single color or sequence. |
|---|
| label | Label for legend. |
|---|
| log | If 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:
- Basic Histogram
- Customized Histogram with Density Plot
- Customized Histogram with Watermark
- Multiple Histograms with Subplots
- Stacked Histogram
- 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: