Visualizing with Multiple Line Graphs in Matplotlib
Line graphs are a go-to for showing trends over time — but what if you have more than one set of data to compare? That's where multiple line graphs come in. In this tutorial, we'll walk you through how to create and customize multiple lines on a single graph using Python's Matplotlib library.
Why Use Multiple Line Graphs?
Multiple line graphs are perfect when you want to compare different data series over the same time period. For example, tracking temperatures in several cities, comparing website traffic across different sources, or seeing how various stocks are performing. Displaying them on the same graph makes it easier to spot patterns, differences, and trends.
What You'll Learn
- Plot multiple line graphs on the same chart to compare different data sets
- Use color, labels, and legends to make each line clear and distinguishable
- Avoid messy overlaps with smart styling and layout tricks
- Understand real-world uses like comparing cricket scores across matches
- Master
plt.plot()with multiple lines — one graph, many stories
Step-by-Step Code Example
Here's how to create a multiple line graph in Matplotlib — we're comparing match scores for Joe Root and Steve Smith across 30 games:
import matplotlib.pyplot as plt
import numpy as np
Joe_Root = np.array([28, 62, 15, 110, 33, 8, 70, 92, 45, 60,
81, 39, 20, 75, 13, 18, 67, 41, 99, 50,
103, 58, 36, 61, 49, 88, 4, 10, 29, 73])
Steve_Smith = np.array([23, 55, 47, 29, 83, 17, 66, 87, 40, 34,
51, 27, 90, 63, 36, 31, 21, 60, 70, 44,
78, 69, 48, 57, 39, 74, 12, 97, 65, 26])
plt.figure(figsize=(10, 5))
x = np.arange(1, 31)
plt.plot(x, Joe_Root, label='Joe Root', color='#184452', marker='o')
plt.plot(x, Steve_Smith, label='Steve Smith', color='#77cce6', marker='o')
plt.title('Joe Root vs Steve Smith')
plt.xlabel('Matches', fontdict={'fontname': 'Comic Sans MS', 'fontsize': 15})
plt.ylabel('Scores', fontdict={'fontname': 'Comic Sans MS', 'fontsize': 15})
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.grid(True, alpha=0.7, linestyle='--')
plt.show()
We've used two arrays — Steve_Smith and Joe_Root — with 30 match scores each (dummy data, don't take it too seriously).
Understanding the Code
- Import libraries:
matplotlib.pyplotfor plotting andnumpyfor handling numerical data. - Define the data: Two arrays with 30 scores each — one per player.
- Set up the X-axis:
np.arange(1, 31)gives match numbers 1 to 30. - Plot the lines:
plt.plot()is called twice — once per player — with different colors and markers. - Labels & title:
plt.xlabel(),plt.ylabel(), andplt.title()give the graph context. - Legend:
plt.legend()places a key outside the graph area so it doesn't overlap the lines. - Grid:
plt.grid()adds light dashed lines — makes values easier to read. - Display:
plt.show()renders everything on screen.
Output:
Two lines, two players, one clean comparison.
When to Use Multiple Line Graphs
- Compare multiple subjects: Two or more individuals, groups, or entities across the same timeline.
- Track changes over time: Different variables changing in parallel over days, months, or years.
- Spot trends and patterns: Identify common trends, divergences, or anomalies between datasets.
- Analyze performance gaps: Show who's leading, catching up, or falling behind.
- Tell a visual story: Show narrative moments — a comeback, a crossover point, or consistent dominance.
Mini Project: Track Your Favorite Players This Season
Ready to flex your Matplotlib skills and your sports obsession? Pick 2–3 of your favorite players from cricket, football, or basketball and track how they performed match by match.
- X-axis: Match number or match dates
- Y-axis: Performance metric — goals, runs, assists, wickets, etc.
- Each player gets a different colored line so you can easily compare their season.
This gives you a cool visual of who's been consistent, who had a comeback, and who's been ghosting.
"Battle of the GOATs: 2025 Season Stats"
Bar Graphs · Customize Graphs · Pie Charts · Histogram · Line Graphs · Scatter Plots