Line charts and bar charts

3.11.2

Use lines for ordered measurements and bars to compare discrete categories.

On this page

Plot an ordered trend

A line connects values in the order supplied. Sort chronological observations before plotting. Markers can distinguish measured points from the lines that connect them.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(["Jan", "Feb", "Mar"], [11.5, 17, 15], marker="o")
ax.set(xlabel="Month", ylabel="Recorded sales")
plt.show()
Recorded sales rise from 11.5 in January to 17 in February, then fall to 15 in March. Download image

Compare categories with bars

bar draws one bar per category and starts at zero by default. Keep a zero baseline for ordinary comparisons. Use barh to keep long category names readable.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(["Tea", "Coffee", "Cake"], [6, 10, 6])
ax.set(ylabel="Units sold", title="Units sold by item")
plt.show()
Tea and cake have six units sold each. Coffee leads with ten. Download image

Official references