Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 11

Bar Charts

Compare separate categories with plt.bar, control colour and width, and understand why a bar chart must always start at zero.

Intermediate25 min
Bar Charts hero Module 25 Lesson 11Bar Charts hero Module 25 Lesson 11

A bar chart compares quantities across categories that have no natural order: teams, products, regions, tracks. Each bar's length is the number, and because the eye compares lengths well, a bar chart is usually the clearest answer to which is bigger, and by how much.

A bar chart of learners enrolled on four tracksA bar chart of learners enrolled on four tracks

Categories and heights

plt.bar(x, height) takes the category labels and the numbers. Matplotlib spaces the bars evenly and writes the labels under them, so text categories need no extra work.

python
import matplotlib.pyplot as plt

tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]

plt.bar(tracks, enrolled)
plt.title("Learners per track")
plt.xlabel("track")
plt.ylabel("learners")
plt.show()

print("largest:", tracks[enrolled.index(max(enrolled))])
print("total:", sum(enrolled))

Output:

text
largest: Python
total: 358

Colour, one or many

One colour string paints every bar. A list of colours paints them one by one, which is worth doing only when the colour means something, such as highlighting the bar you are writing about. A rainbow of colours that carry no meaning just slows a reader down.

python
import matplotlib.pyplot as plt

tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]

plt.bar(tracks, enrolled, color="#2563EB")
plt.title("One colour: the lengths do the talking")
plt.show()

quiet = "#CBD5E1"
shades = [quiet if track != "pandas" else "#7C3AED" for track in tracks]
plt.bar(tracks, enrolled, color=shades)
plt.title("Colour used to point at one bar")
plt.show()

print("highlighted:", "pandas")

Output:

text
highlighted: pandas
A bar chart with the third bar highlighted and the rest greyA bar chart with the third bar highlighted and the rest grey

Width and gaps

width is measured in category slots, and the default is 0.8, which leaves a small gap between bars. Narrower bars give more white space; a width of 1 removes the gaps entirely and starts to look like a histogram, which is a different chart with a different meaning.

python
import matplotlib.pyplot as plt

tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]

plt.bar(tracks, enrolled, width=0.5, color="#7C3AED")
plt.title("width=0.5")
plt.show()

print("default width:", 0.8)
print("gaps say the categories are separate things")

Output:

text
default width: 0.8
gaps say the categories are separate things

Sorting and a fair zero

Two habits make bar charts honest and easy to read. Sort the bars by size unless the categories have their own order, so the ranking is visible. And leave the value axis starting at zero, because a bar that starts at 80 is telling the reader its length means something it does not.

python
import matplotlib.pyplot as plt

tracks = ["Python", "NumPy", "pandas", "Plotting"]
enrolled = [120, 86, 94, 58]

ordered = sorted(zip(enrolled, tracks), reverse=True)
heights = [value for value, _ in ordered]
labels = [name for _, name in ordered]

plt.bar(labels, heights, color="#7C3AED")
plt.title("Sorted, and starting at zero")
plt.ylabel("learners")
plt.grid(axis="y", alpha=0.3)
plt.show()

for name, value in zip(labels, heights):
    print(f"{name:<9} {value}")

Output:

text
Python    120
pandas    94
NumPy     86
Plotting  58

Key takeaways

  • plt.bar(x, height) compares categories that have no natural order

  • One colour string paints every bar; a list paints them individually

  • Use colour to point at a bar, not to decorate every one

  • Sort by size, and always start the value axis at zero

  • Next: Horizontal Bar Charts

  • Previous: Working with Figure and Axes Objects

Loading playground…

Check your understanding

Bar Charts Quiz

What is a bar chart for?

1 / 10