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




Step-by-Step Code Example

Here’s a simple example of how to create a multiple line graph in Matplotlib:

Python

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





When to Use Multiple Line Graphs




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.

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:






More educational content :