Skip to content

Line Plot

When to use line plots

Line plots are best for:

  • Trends over time
  • Comparing changes across periods

A line plot is Matplotlib’s default: plt.plot(data)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)(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.

Basic line plot

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

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

Visualize it

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.

Tip

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

Next

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

🧪 Try It Yourself

Exercise 1 – Plot with a marker

Exercise 2 – Label a series for the legend

Exercise 3 – Step drawstyle

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did