Lesson 6: Line Charts

⏱ ~25 min Lesson 6 of 10 💚 Free

Line charts show how a value changes over time. Temperature over a week, sales by month, scores across a semester — any time you have data at regular intervals, a line chart is your first choice.

Key Concepts

When to Use a Line Chart

Use a line chart when the x-axis represents ordered time or sequence, and you want to show trends (going up, going down, staying flat). Don't use it for unordered categories.

The matplotlib.pyplot API

import matplotlib.pyplot as plt
plt.plot(x_vals, y_vals, color="blue", marker="o")
plt.title("My Chart")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.show()

Multiple Lines

Call plt.plot() multiple times before plt.show() to add more lines. Use the label= parameter and plt.legend() to identify each line.

Styling

plt.plot(x, y, color="#4ade80", linewidth=2, linestyle="--", marker="s")
Colors: hex codes or names. linestyle: "-", "--", ":", "-."
markers: "o", "s", "^", "D"

🔬 Interactive Lab: Line Chart Builder

Click on the canvas to add data points. The line chart draws in real time. Double-click to clear.

✅ Check Your Understanding

1. When is a line chart most appropriate?

2. In matplotlib, which function draws a line chart?

3. What does plt.legend() do?