Lesson 18
Histogram Styles
Switch between the bar, step, and stepfilled histogram styles, thin the bars with rwidth, and set the fill and edge colours.
The default histogram is a row of filled bars, and that is the right choice most of the time. Two other styles earn their place when the chart gets crowded: step draws the outline only, and stepfilled fills that outline without drawing a border between neighbouring bins.
The three you will use
histtype chooses the style. Side by side the difference is obvious: bar separates each bin, step reduces the chart to a line you can see through, and stepfilled sits in between.
import matplotlib.pyplot as plt
minutes = [
22, 24, 25, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 34, 35,
35, 36, 36, 37, 38, 38, 39, 40, 41, 42, 44, 46, 48, 52,
]
plt.figure(figsize=(11, 4))
for index, kind in enumerate(["bar", "step", "stepfilled"], start=1):
plt.subplot(1, 3, index)
plt.hist(minutes, bins=6, histtype=kind, color="#2DD4BF", edgecolor="#0F172A")
plt.title(kind)
plt.tight_layout()
plt.show()
print("same counts, three outlines")Output:
same counts, three outlinesThinner bars with rwidth
rwidth is the fraction of each bin the bar actually fills, so rwidth=0.8 leaves a fifth of every bin as white space. It only affects the bar style, and it is worth knowing that the gaps it creates make a histogram look like a bar chart, which is a little misleading.
import matplotlib.pyplot as plt
minutes = [
22, 24, 25, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 34, 35,
35, 36, 36, 37, 38, 38, 39, 40, 41, 42, 44, 46, 48, 52,
]
plt.hist(minutes, bins=6, histtype="bar", rwidth=0.85, color="#7C3AED")
plt.title("rwidth leaves a gap in each bin")
plt.xlabel("minutes")
plt.ylabel("deliveries")
plt.show()
print("rwidth is a fraction of the bin, not a number of units")Output:
rwidth is a fraction of the bin, not a number of unitsFill and edge
color fills the bars and edgecolor outlines them. An outline is close to essential on the bar style: without it, neighbouring bars of the same colour merge into one block and you cannot see where a bin ends. For step there is no fill, so the colour is the line.
import matplotlib.pyplot as plt
minutes = [
22, 24, 25, 27, 28, 28, 29, 30, 31, 31, 32, 33, 33, 34, 34, 35,
35, 36, 36, 37, 38, 38, 39, 40, 41, 42, 44, 46, 48, 52,
]
plt.subplot(1, 2, 1)
plt.hist(minutes, bins=8, color="#2DD4BF")
plt.title("no edge")
plt.subplot(1, 2, 2)
plt.hist(minutes, bins=8, color="#2DD4BF", edgecolor="#0F172A", linewidth=1.2)
plt.title("with an edge")
plt.tight_layout()
plt.show()
print("the right chart shows where each bin ends")Output:
the right chart shows where each bin endsStep for comparing distributions
step comes into its own with two or three distributions on one chart. Filled bars hide each other however much transparency you add; outlines simply cross, and every shape stays readable.
import matplotlib.pyplot as plt
edges = [20, 26, 32, 38, 44, 50, 56]
morning = [22, 25, 27, 28, 30, 31, 33, 34, 35, 36, 38, 41, 44, 48]
evening = [26, 29, 31, 33, 34, 36, 37, 39, 40, 42, 43, 45, 49, 53]
plt.hist(morning, bins=edges, histtype="step", linewidth=2.5,
color="#7C3AED", label="morning")
plt.hist(evening, bins=edges, histtype="step", linewidth=2.5,
color="#F59E0B", label="evening")
plt.title("Two outlines, both readable")
plt.xlabel("minutes")
plt.ylabel("deliveries")
plt.legend()
plt.show()
print("no transparency needed")Output:
no transparency neededKey takeaways
-
histtype picks bar, step, stepfilled, or barstacked
-
rwidth thins the bars as a fraction of the bin, on the bar style only
-
Always give bar histograms an edgecolor so the bins stay visible
-
step is the best style for laying two distributions over each other
-
Next: Pie Charts
-
Previous: Choosing Histogram Bins
Loading playground…
Check your understanding
Histogram Styles Quiz
Which argument chooses the histogram style?
1 / 10