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 or sequence. For example, you might want to track the temperature in several cities, compare website traffic across different sources, or see 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 & legends to make each line clear and distinguishable
- Learn how to avoid messy overlaps with smart styling and layout tricks
- Understand real-world uses like comparing crime trends across years or regions
- Master
plt.plot()
with multiple lines — one graph, many stories
Step-by-Step Code Example
Here’s a simple example of how to create a multiple line graph in Matplotlib:
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 taken two arrays named Steve_Smith and Joe_Root, which contain the scores of these two players (don’t take it too seriously — it’s just dummy data).
Understanding the Code Step-by-Step
-
Import libraries:
matplotlib.pyplot
(akaplt
) is for plotting pretty graphs, andnumpy
(akanp
) helps us handle numerical data like a pro. -
Define the data: We’ve got two arrays here —
Joe_Root
andSteve_Smith
— each with 30 scores (probably match runs). These are the stats we’ll be visualizing. -
Set up the X-axis:
np.arange(1, 31)
gives us a range of match numbers from 1 to 30. This is the base for our X-axis — each number repping a match. -
Plot the lines:
plt.plot()
is doing the magic here. We plot both players’ scores with different colors and markers so we can tell them apart easily. Root gets a deep blue, and Smith gets a chill light blue. - Add labels & title: We slap on a title and label the X and Y axes using a fun font (Comic Sans MS, of course). This helps make the graph way more readable.
-
Legend time:
plt.legend()
drops a little key to the side so we know which line is whose. Positioned top-right but outside the main graph area. -
Grid it out: The
plt.grid()
function adds light dashed lines across the plot. Makes everything easier to read — and lowkey aesthetic. -
Show it off:
plt.show()
finally displays the graph on screen. Boom — data visualized!
When to Use Multiple Line Graphs
- Compare multiple subjects: Useful when comparing two or more individuals, groups, or entities — like players, products, or locations — across the same timeline.
- Track changes over time: When you want to visualize how different variables change in parallel over days, months, or years.
- Spot trends and patterns: Helps in identifying common trends, divergences, or anomalies between datasets.
- Analyze performance gaps: Ideal for showing who’s leading, catching up, or falling behind (e.g. team rankings or revenue comparisons).
- Tell a story visually: Multiple lines on one graph can clearly show narrative moments — like a comeback, a crossover point, or consistent dominance.
Here's the output :

Ommo, this looks so cool!
🔹Mini Project: Track Your Favorite Players This Season
Ready to flex your matplotlib skills and your sports obsession? Here’s a graph idea that’s way more fun:
Pick 2–3 of your favorite players (from cricket, football, basketball—whatever you vibe with) and track how they performed match by match.
- X-axis: Match number (or match dates)
- Y-axis: Performance metric — like goals, runs, assists, wickets, etc.
- Each player gets a different colored line so you can easily compare their season.
This graph gives you a cool visual of who's been consistent, who had a comeback, and who’s been ghosting.
Bonus points if you:
- Add team logos as markers (if you're feeling spicy)
- Highlight best & worst games with annotations
- Use a title like
“Battle of the GOATs: 2025 Season Stats”
More educational content :