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
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact