Skip to content

Line Plot

Line plots are best for:

  • Trends over time
  • Comparing changes across periods

A line plot is Matplotlib’s default: plt.plot(data) alone draws a line, connecting each point to the next in the order the data appears. Points aren’t interpolated by magic — Matplotlib just draws straight segments between consecutive (x, y) pairs, so ordering your data matters.

diagram Building a line plot mermaid
How x/y arrays become a line, then a labelled, legible chart.
Line plot
import matplotlib.pyplot as plt
 
x = [1, 2, 3, 4, 5]
y = [120, 140, 130, 160, 155]
 
plt.figure(figsize=(7, 4))
plt.plot(x, y, marker="o")
plt.title("Orders over time")
plt.xlabel("Day")
plt.ylabel("Orders")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Multiple lines
import matplotlib.pyplot as plt
 
x = [1, 2, 3, 4, 5]
y1 = [120, 140, 130, 160, 155]
y2 = [100, 110, 120, 125, 140]
 
plt.figure(figsize=(7, 4))
plt.plot(x, y1, label="Product A")
plt.plot(x, y2, label="Product B")
 
plt.title("Orders over time")
plt.xlabel("Day")
plt.ylabel("Orders")
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()

Watch a line plot draw itself point by point — each new value connects to the previous one with a straight segment:

sketch A line plot connects points in order p5.js
Matplotlib draws a straight segment between each consecutive (x, y) pair as it walks through the data.

If lines cross and the plot becomes confusing, consider faceting (multiple subplots) or focusing on fewer series.

Continue to Bar Chart and Horizontal Bar to compare categories instead of trends.

Exercise 2 – Label a series for the legend

Section titled “Exercise 2 – Label a series for the legend”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading