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.
flowchart LR A["x values"] --> C["plt.plot(x, y)"] B["y values"] --> C C --> D["marker + linestyle"] D --> E["title / xlabel / ylabel"] E --> F["legend (if multiple series)"]
Basic 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()Visualize it
Watch a line plot draw itself point by point — each new value connects to the previous one with a straight segment:
Tip
If lines cross and the plot becomes confusing, consider faceting (multiple subplots) or focusing on fewer series.
Check your understanding
What are line plots best for?
Lines connect consecutive points in order, so they show how something changes across periods.
Show answer
A — Trends over time — Lines connect consecutive points in order, so they show how something changes across periods.
What does marker="o" do in plt.plot(x, y, marker="o")?
marker adds a symbol at each (x, y) point on top of the connecting line.
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.
Why does the order of your data matter in a line plot?
Points are not interpolated by magic, so unsorted data produces a zigzag line.
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.
What does the lesson suggest if many lines cross and the plot is confusing?
Fewer series or small multiples keep each trend readable.
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
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Label a series for the legend
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Step drawstyle
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.