Matplotlib Bars

plt.bar() and plt.barh() draw vertical or horizontal bars whose length represents a value for each category, making them ideal for comparing totals across groups.

When to Use a Bar Chart

Bar charts compare a value across a small number of discrete categories, such as sales per region, votes per candidate, or errors per software module. Because each bar's length is judged against a shared baseline, bar charts make it easy to see which category is biggest, smallest, or roughly tied, which is harder to do from a table of numbers alone.

Vertical Bars with bar()

plt.bar(x, height) draws one vertical bar per entry in x, with the bar's height taken from the matching entry in height. x is usually a list of category names or positions, not a continuous numeric range.

Example

import matplotlib.pyplot as plt
import numpy as np

languages = np.array(["Python", "JavaScript", "Java", "C++", "Go"])
survey_votes = np.array([145, 120, 95, 60, 40])

plt.bar(languages, survey_votes, color="steelblue")
plt.xlabel("Language")
plt.ylabel("Votes")
plt.title("Favorite programming language")
plt.show()

Horizontal Bars with barh()

plt.barh(y, width) works the same way but draws horizontal bars, which is useful when category names are long and would overlap if squeezed under vertical bars. The first argument is still the categories, but now the second argument controls bar length along the x-axis rather than height.

Example

import matplotlib.pyplot as plt
import numpy as np

departments = np.array(["Customer Support", "Engineering", "Sales", "Marketing", "Finance"])
open_tickets = np.array([32, 12, 27, 18, 9])

plt.barh(departments, open_tickets, color="darkorange", height=0.6)
plt.xlabel("Open tickets")
plt.title("Open tickets by department")
plt.show()
  • color sets a single bar color, or accepts a list to color each bar individually.
  • width (for bar()) or height (for barh()) controls how thick each bar is drawn, as a fraction of the space between categories; the default is 0.8.
  • edgecolor draws an outline around each bar, which helps separate bars that share a similar color.
  • yerr (for bar()) or xerr (for barh()) adds error bars showing a margin of uncertainty above and below each bar.

Example

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(4)
quarter_labels = np.array(["Q1", "Q2", "Q3", "Q4"])
revenue_2024 = np.array([50, 62, 58, 70])
revenue_2025 = np.array([55, 65, 68, 80])

plt.bar(x - 0.2, revenue_2024, width=0.4, label="2024")
plt.bar(x + 0.2, revenue_2025, width=0.4, label="2025")
plt.xticks(x, quarter_labels)
plt.ylabel("Revenue ($1000s)")
plt.title("Quarterly revenue comparison")
plt.legend()
plt.show()
Note: Matplotlib has no built-in "grouped bar" mode. The trick above shifts one series left and the other right by a small offset (0.2) and gives both a narrower width (0.4) so the two bars per quarter sit side by side without overlapping.
FunctionCategory axisValue axis
plt.bar(x, height)x-axis (horizontal)y-axis (vertical, the bar height)
plt.barh(y, width)y-axis (vertical)x-axis (horizontal, the bar length)

Exercise: Matplotlib Bars

What does plt.bar() primarily help compare?