Line Plot
When to use line plots
Section titled “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
Section titled “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
Section titled “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
Section titled “Visualize it”Watch a line plot draw itself point by point — each new value connects to the previous one with a straight segment:
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.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Plot with a marker
Section titled “Exercise 1 – Plot with a marker”Exercise 2 – Label a series for the legend
Section titled “Exercise 2 – Label a series for the legend”Exercise 3 – Step drawstyle
Section titled “Exercise 3 – Step drawstyle”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading