Line Plot
When to use line plots
Line plots are best for:
- Trends over time
- Comparing changes across periods
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()Tip
If lines cross and the plot becomes confusing, consider faceting (multiple subplots) or focusing on fewer series.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
