VOOZH about

URL: https://www.geeksforgeeks.org/data-visualization/plotting-histogram-in-python-using-matplotlib/

⇱ Plotting Histogram in Python using Matplotlib - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

  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

Output:

👁 Histogram in Python using Matplotlib

Explanation:

  • Generates 1000 random numbers from a standard normal distribution.
  • Plots a histogram with 30 bins, sky-blue bars and black edges.
  • Adds X and Y axis labels and a title.
  • Displays the histogram plot.

This is the simplest way to visualize data distribution.

2. Customized Histogram with Density Plot

Output:

👁 Histogram Matplotlib

Explanation:

  • Generates 1000 random numbers.
  • Plots a histogram with 30 bins and a smooth density curve (KDE) using Seaborn.
  • Colors bars light green with red edges.
  • Adds axis labels and a title.
  • Displays the plot showing data distribution and density.

3. Customized Histogram with Watermark

Output:

👁 plot

Explanation:

  • Sets the random seed for reproducibility.
  • Generates 10,000 random numbers (x) from a standard normal distribution.
  • Plots a histogram with 20 bins and applies a color gradient to the bars.
  • Removes axis spines and ticks, adds gridlines and includes a watermark.
  • Adds axis labels, a legend and a title.
  • Displays a visually enhanced, customized histogram.

4. Multiple Histograms with Subplots

Output:

👁 Screenshot-2023-12-05-222526

Explanation:

  • Generates two datasets of 1,000 random numbers each.
  • Creates side-by-side histograms using subplots.
  • Plots data1 in yellow and data2 in pink with 30 bins.
  • Adds axis labels and titles, adjusts layout and displays the figure.

5. Stacked Histogram

Output:

👁 Screenshot-2023-12-05-222933

Explanation:

  • Generates two datasets of 1,000 random numbers each.
  • Creates a stacked histogram showing both datasets combined.
  • Uses cyan and purple bars with black edges.
  • Adds axis labels, a title and a legend.
  • Displays the histogram showing combined data distribution.

6. 2D Histogram (Hexbin Plot)

Output:

👁 2D_histogram
Output

Explanation:

  • Generates two sets of 1,000 random numbers (x and y).
  • Creates a 2D histogram (hexbin plot) showing data density with hexagons.
  • Uses a blue color map to represent density intensity.
  • Adds axis labels, a title and a colorbar to interpret density.
  • Displays the 2D histogram showing the relationship between x and y.

Related Articles

Comment