Histograms

Matplotlib Basics

1 min read

Published Oct 5 2025


15
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonVisualisation

A histogram visualises the distribution of numerical data — it shows how many data points fall into a range of values (called bins).

Instead of plotting individual points, it groups values and displays the frequency in vertical bars.


Syntax:

plt.hist(x, bins=None, range=None, density=False, color=None, edgecolor=None, alpha=None, histtype='bar')

Parameters:

  • x = Data to plot
  • bins = Number of bins (int) or explicit bin edges
  • range = Lower and upper range of bins
  • density = Normalise histogram (area = 1)
  • color = Fill colour
  • edgecolor = Outline color
  • alpha = Transparency
  • histtype = Type of histogram: 'bar', 'step', 'stepfilled'
  • label = Legend label



Basic histogram example

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data)
plt.title("Basic Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

This automatically chooses 10 bins by default.


matplotlib histogram chart basic example




Custom number of bins

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=20, color='skyblue', edgecolor='black')
plt.title("Histogram with 20 Bins")
plt.show()

matplotlib histogram chart custom bins example




Define custom bin edges

You can define the exact cut points:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)
bins = [-3, -2, -1, 0, 1, 2, 3]

plt.hist(data, bins=bins, color='salmon', edgecolor='black')
plt.title("Custom Bin Edges")
plt.show()

matplotlib histogram chart custom bin edges example




Change Colour and Edge

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=15, color='orange', edgecolor='blue')
plt.title("Styled Histogram")
plt.show()

matplotlib histogram chart styled example




Multiple histograms on the same plot

You can overlay histograms to compare distributions:

import matplotlib.pyplot as plt
import numpy as np

data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1, 1000)

plt.hist(data1, bins=20, alpha=0.5, label='Group A')
plt.hist(data2, bins=20, alpha=0.5, label='Group B')
plt.legend()
plt.title("Multiple Histograms (Overlayed)")
plt.show()

Use alpha for transparency so both remain visible.


matplotlib histogram chart multiple example




Stacked Histograms

Stack multiple datasets to see combined distribution:

import matplotlib.pyplot as plt
import numpy as np

data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1, 1000)

plt.hist([data1, data2], bins=20, stacked=True, color=['skyblue', 'lightcoral'], label=['Group A', 'Group B'])
plt.legend()
plt.title("Stacked Histogram")
plt.show()

matplotlib histogram chart stacked example



Step and step-filled styles

Step:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30, histtype='step', color='navy', linewidth=2)
plt.title("Step Histogram")
plt.show()

matplotlib histogram chart step example

Step Filled:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30, histtype='stepfilled', color='lightgreen', alpha=0.7)
plt.title("Step-Filled Histogram")
plt.show()

matplotlib histogram chart step filled example





Normalised histogram (density plot)

If you want the area under the curve = 1, use density=True. This is common for probability distributions.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30, density=True, color='plum', edgecolor='black')
plt.title("Normalised Histogram (Density=True)")
plt.ylabel("Probability Density")
plt.show()

matplotlib histogram chart density example




Cumulative Histogram

To show cumulative frequencies.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30, cumulative=True, color='lightblue', edgecolor='black')
plt.title("Cumulative Histogram")
plt.show()

matplotlib histogram chart cumulative example




Horizontal histogram

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=20, orientation='horizontal', color='lime', edgecolor='black')
plt.title("Horizontal Histogram")
plt.show()

matplotlib histogram chart horizontal example

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact