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()
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()
Download image