#124 Spaghetti plot






A Spaghetti plot is a line plot with many lines displayed together. The problem is that it is really hard to read, and thus provide few insight about the data. This is well documented here.

This page explains how to realise it with python and, more importantly, provide a few propositions to make it better.

Let’s start by realising it:





Spaghetti plot code

# libraries and data
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Make a data frame
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), 'y10': np.random.randn(10)+range(2,12) })

# style

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

# multiple line plot
for column in df.drop('x', axis=1):
plt.plot(df['x'], df[column], marker='', color=palette(num), linewidth=1, alpha=0.9, label=column)

# Add legend
plt.legend(loc=2, ncol=2)

# Add titles
plt.title("A (bad) Spaghetti plot", loc='left', fontsize=12, fontweight=0, color='orange')


Other way to represent these data

#123 Highlight a group in line chart




Highlight a group

Let’s say you plot many group, but the actual reason for that is to explain the feature of one particular group compared to the others.

Then a good practice is to highlight this group: make it appear different, and give it a proper annotation. Here, the behaviour of the orange line is obvious.

See the code here.















Use small multiples

If all groups interest you, a good solution would be to split them in separate subplots. As you can see here, the behaviour of each group is much more readable that before.

See the code of this version here.


















Small multiples (variant)

Another option consists to do the same but display all the groups on each subplot discreetly.

It’s up to you to choose the version you prefer. Here is the code.












Area chart 

If you decide to use small multiples, I have rea personal preference for area chart instead of line plot. I find it easier to see the trends, but it is a personal opinion.

In any case, here is the code of this chart.








  • Sponsors


    Leave a Reply

    Your email address will not be published.