Python Central HubSign in

Data Analytics with Python / Phase 5: Data Visualization with Matplotlib

Line Plot

Plot trends over time, compare multiple series, and make line charts readable.

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

Basic line plot

Python
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

Python
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.

Check your understanding

quizCheck yourself
  1. What are line plots best for?

    Show answer

    A — Trends over time — Lines connect consecutive points in order, so they show how something changes across periods.

  2. What does marker="o" do in plt.plot(x, y, marker="o")?

    Show answer

    A — Draws a circle at each data point — marker adds a symbol at each (x, y) point on top of the connecting line.

  3. Why does the order of your data matter in a line plot?

    Show answer

    A — Matplotlib draws straight segments between consecutive (x, y) pairs in the order given — Points are not interpolated by magic, so unsorted data produces a zigzag line.

  4. What does the lesson suggest if many lines cross and the plot is confusing?

    Show answer

    A — Use faceting with multiple subplots or show fewer series — Fewer series or small multiples keep each trend readable.

Next

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

🧪 Try It Yourself

Exercise 1 – Plot with a marker

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Label a series for the legend

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Step drawstyle

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did