Bar charts compare a value across discrete categories — each category gets one bar,
and the bar's height (or length, for horizontal bars) shows its value. As McKinney
notes, plot.bar() and plot.barh() even work directly with a labeled Series or
DataFrame: the index becomes the category ticks automatically.
flowchart LR
A["Categories + values"] --> B{"Long labels?"}
B -->|No| C["plt.bar() - vertical bars"]
B -->|Yes| D["plt.barh() - horizontal bars"]
C --> E["Sort by value for easy scanning"]
D --> E
Basic bar chart
import matplotlib.pyplot as plt
cities = ["Pune", "Delhi", "Mumbai", "Bengaluru"]
sales = [120, 180, 90, 160]
plt.figure(figsize=(7, 4))
plt.bar(cities, sales)
plt.title("Sales by city")
plt.xlabel("City")
plt.ylabel("Sales")
plt.xticks(rotation=20)
plt.tight_layout()
plt.show()Sorted bars
import matplotlib.pyplot as plt
data = {"Pune": 120, "Delhi": 180, "Mumbai": 90, "Bengaluru": 160}
items = sorted(data.items(), key=lambda x: x[1], reverse=True)
labels = [k for k, _ in items]
values = [v for _, v in items]
plt.figure(figsize=(7, 4))
plt.bar(labels, values)
plt.title("Sales by city (sorted)")
plt.tight_layout()
plt.show()Horizontal bars
import matplotlib.pyplot as plt
labels = ["Very long category A", "Long B", "C"]
values = [12, 19, 7]
plt.figure(figsize=(7, 4))
plt.barh(labels, values)
plt.title("Horizontal bar chart")
plt.xlabel("Value")
plt.tight_layout()
plt.show()Visualize it
Watch each category's bar grow to its value — the tallest bar is the easiest to spot because bars share the same zero baseline:
Tip
Start y-axis at 0 for bar charts to avoid misleading comparisons.
Check your understanding
Which function draws horizontal bars?
plt.barh is the horizontal version of plt.bar. It is helpful when category names are long.
Show answer
A — plt.barh — plt.barh is the horizontal version of plt.bar. It is helpful when category names are long.
How does the lesson sort the city sales dict from highest to lowest?
Sorting the items by their value (x[1]) in reverse gives Delhi 180, Bengaluru 160, Pune 120, Mumbai 90.
Show answer
A — sorted(data.items(), key=lambda x: x[1], reverse=True) — Sorting the items by their value (x[1]) in reverse gives Delhi 180, Bengaluru 160, Pune 120, Mumbai 90.
What does plt.xticks(rotation=20) do on the Sales by city chart?
Rotating tick labels keeps longer names such as Bengaluru from overlapping.
Show answer
A — Rotates the category labels by 20 degrees — Rotating tick labels keeps longer names such as Bengaluru from overlapping.
What tip does the lesson give about the y-axis of a bar chart?
Bars share a zero baseline, and truncating the axis exaggerates differences.
Show answer
A — Start it at 0 to avoid misleading comparisons — Bars share a zero baseline, and truncating the axis exaggerates differences.
Next
Continue to Histogram to visualize the distribution of a single numeric column.
🧪 Try It Yourself
Exercise 1 – Draw vertical bars
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Horizontal bars for long labels
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Sort before plotting
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.