Libraries & Data

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame({
   'x': range(1,11),
   'y1': np.random.randn(10),
   'y2': np.random.randn(10)+range(1,11),
   'y3': np.random.randn(10)+range(11,21),
   'y4': np.random.randn(10)+range(6,16),
   'y5': np.random.randn(10)+range(4,14)+(0,0,0,0,0,0,0,-3,-8,-6),
   'y6': np.random.randn(10)+range(2,12),
   'y7': np.random.randn(10)+range(5,15),
   'y8': np.random.randn(10)+range(4,14),
   'y9': np.random.randn(10)+range(4,14) 
})
df.head()
xy1y2y3y4y5y6y7y8y9
010.3152131.60734310.2044265.6265652.452983-0.4394785.8269525.5168633.586473
12-1.1501702.02858012.0182956.6872476.6051402.8340756.6737205.0400396.325240
230.2043893.08649612.6354507.5213374.1215833.3336967.0809586.6814835.167516
340.9033282.36293513.4309458.5578506.9466874.9300416.0898587.5187447.458009
45-1.4774834.96141513.9590169.7134297.5933936.2340289.3268027.9861088.271259

Spaghetti Chart

Several ways exist to avoid creating a spaghetti plot, and one of them is using small multiple: here we cut the window in several subplots, one per group.

Then, you can choose to display every group discretely, or not to show them at all. Note that you can easily do the same with the area chart.

Left graph

You can create multiple plots on the same figure by using the subplot() function of matplotlib.

Note that you can check this post to see more examples of the subplot() function.

Small multiples for line chart: Left graph

# Create a color palette
palette = plt.get_cmap('Set1')

# Create a figure and axes
fig, axes = plt.subplots(3, 3, figsize=(12, 12), dpi=150)

# Flatten the axes array for easy iteration
axes = axes.flatten()

# Multiple line plots
for num, column in enumerate(df.drop('x', axis=1), start=1):
    ax = axes[num-1]
    
    # Plot the line plot
    ax.plot(df['x'], df[column], marker='', color=palette(num), linewidth=1.9, alpha=0.9, label=column)
    
    # Same limits for every chart
    ax.set_xlim(0, 10)
    ax.set_ylim(-2, 22)
    
    # Not ticks everywhere
    if num in range(7):
        ax.tick_params(labelbottom=False)
    if num not in [1, 4, 7]:
        ax.tick_params(labelleft=False)
    
    # Add title
    ax.set_title(column, loc='left', fontsize=12, fontweight=0, color=palette(num))

# General title
fig.suptitle(
    "How the 9 students improved\nthese past few days?",
    fontsize=13,
    fontweight=0,
    color='black',
    style='italic',
    y=1.02
)

# Adjust layout
fig.tight_layout(rect=[0, 0.03, 1, 0.95])

# Show the graph
plt.show()

Right graph

As an alternative to the previous example, you can make multiple subplots consisting the lines of all groups with only one group highlighted in each subplot.

Small multiples for line chart: Right graph

# Create a color palette
palette = plt.get_cmap('Set1')

# Create a figure and axes
fig, axes = plt.subplots(3, 3, figsize=(12, 12))

# Flatten the axes array for easy iteration
axes = axes.flatten()

# Multiple line plots
for num, column in enumerate(df.drop('x', axis=1), start=1):
    ax = axes[num-1]
    
    # Plot every group, but discrete (lower alpha)
    for v in df.drop('x', axis=1):
        ax.plot(df['x'], df[v], marker='', color='grey', linewidth=0.6, alpha=0.3)
    
    # Plot the line plot
    ax.plot(df['x'], df[column], marker='', color=palette(num), linewidth=2.4, alpha=0.9, label=column)
    
    # Same limits for every chart
    ax.set_xlim(0, 10)
    ax.set_ylim(-2, 22)
    
    # Not ticks everywhere
    if num in range(7):
        ax.tick_params(labelbottom=False)
    if num not in [1, 4, 7]:
        ax.tick_params(labelleft=False)
    
    # Add title
    ax.set_title(column, loc='left', fontsize=12, fontweight=0, color=palette(num))

# General title
fig.suptitle(
    "How the 9 students improved\nthese past few days?",
    fontsize=13,
    fontweight=0,
    color='black',
    style='italic',
    y=1.02
)

# Adjust layout
fig.tight_layout(rect=[0, 0.03, 1, 0.95])

# Show the graph
plt.show()

Going further

You might be interested in: