Python Central HubSign in

Data Analytics with Python / Phase 5: Data Visualization with Matplotlib

Histogram

Visualize distributions, choose bins, and interpret skew and outliers.

A histogram is really a bar plot in disguise: McKinney describes it as "a discretized display of value frequency." Your values get split into evenly spaced bins, and the bar height for each bin is simply how many values landed inside it.

diagram From raw values to a histogram mermaid
Values are split into evenly spaced bins, then a bar is drawn per bin showing how many values fall inside it.

Basic histogram

Python
import matplotlib.pyplot as plt
 
values = [55, 60, 62, 63, 65, 67, 70, 72, 76, 80, 81, 85, 90, 92, 95]
 
plt.figure(figsize=(7, 4))
plt.hist(values, bins=8, edgecolor="black")
plt.title("Distribution")
plt.xlabel("Value")
plt.ylabel("Count")
plt.tight_layout()
plt.show()

Try different bins

Python
import matplotlib.pyplot as plt
 
fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True)
 
axes[0].hist(values, bins=4, edgecolor="black")
axes[0].set_title("bins=4")
 
axes[1].hist(values, bins=12, edgecolor="black")
axes[1].set_title("bins=12")
 
plt.tight_layout()
plt.show()

Visualize it

A histogram sorts your values into bins and draws a bar for each — the bar's height is how many values landed in that bin. Together the bars reveal the shape of the distribution (here, a bell centred near the middle):

sketch A histogram bins your data p5.js
Each value falls into a bin; the bar height is the count in that bin, revealing the distribution's shape.

Tip

Histograms are a first check before transformations (like log scaling).

Check your understanding

quizCheck yourself
  1. What does the height of a histogram bar represent?

    Show answer

    A — How many values fall inside that bin — Values are split into evenly spaced bins and each bar's height is the count in its bin.

  2. What does bins=8 do in plt.hist(values, bins=8, edgecolor="black")?

    Show answer

    A — Splits the range into 8 bins, and edgecolor outlines each bar — bins sets how many intervals to use, and edgecolor draws black edges so the bars are easy to tell apart.

  3. What does the lesson show when comparing bins=4 with bins=12?

    Show answer

    A — The shape of the histogram changes with the number of bins — Too few bins hide detail and too many bins look noisy, so try different values.

  4. A histogram is a first check before which kind of step?

    Show answer

    A — Transformations like log scaling — Looking at the distribution tells you whether a transform may help.

Next

Continue to Scatter Plot to check the relationship between two numeric variables.

🧪 Try It Yourself

Exercise 1 – Set the bin count

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Read the bin counts

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Compare bin widths

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did