This Matplotlib Histograms lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
A histogram counts how many values fall into each bin. It shows the shape of a list of numbers, not one bar per category name.
The example bins scores by tens.
Example 1: Start here
plt.hist(scores, bins=4) lets Matplotlib choose the edges.
scores = [61, 64, 72, 75, 81, 90] # a list; the values stay in this order
bins = {} # a dictionary; each key has one value
for score in scores: # repeat the indented lines once per value
bucket = (score // 10) * 10 # a tuple; a fixed group of values
bins[bucket] = bins.get(bucket, 0) + 1 # change this one field
print(bins)
Example 2: List the points
Matplotlib Histograms draws points. Printing each pair lets you check the data before a chart library plots it.
points = [(1, 12), (2, 12), (3, 18)] # a list; the values stay in this order
for x, y in points: # repeat the indented lines once per value
print(x, y) # a comma prints each value, with a space between
Example 3: Find the highest bar
The tallest bar is the largest number. max does that lookup.
heights = [12, 12, 18] # a list; the values stay in this order
print(max(heights)) # the largest value
Example 4: Label each point
A chart label belongs with its number. zip keeps each day next to its height.
labels = ["Mon", "Tue", "Wed"] # a list; the values stay in this order
heights = [12, 12, 18] # a list; the values stay in this order
for label, height in zip(labels, heights): # repeat the indented lines once per value
print(label, height) # a comma prints each value, with a space between
