Python Central HubSign in

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

Bar Chart and Horizontal Bar

Compare categories with bar charts, sort values, and use horizontal bars for long labels.

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.

diagram Vertical vs horizontal bars mermaid
Both encode a value as a bar's length; horizontal bars just swap which axis carries the category.

Basic bar chart

Python
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

Python
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

Python
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:

sketch A bar chart compares categories p5.js
Each bar's height is proportional to its value, starting from a shared zero baseline.

Tip

Start y-axis at 0 for bar charts to avoid misleading comparisons.

Check your understanding

quizCheck yourself
  1. Which function draws horizontal bars?

    Show answer

    A — plt.barh — plt.barh is the horizontal version of plt.bar. It is helpful when category names are long.

  2. How does the lesson sort the city sales dict from highest to lowest?

    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.

  3. What does plt.xticks(rotation=20) do on the Sales by city chart?

    Show answer

    A — Rotates the category labels by 20 degrees — Rotating tick labels keeps longer names such as Bengaluru from overlapping.

  4. What tip does the lesson give about the y-axis of a bar chart?

    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

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 – Horizontal bars for long labels

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 – Sort before plotting

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