Line charts are common data visualisation tools. They are usually the first chart we reach for when we want to visualise something that changes with time, depth (e.g well logs) or any other ordered axis. They are great when we want to display a handful of lines, however, when we start adding more than several onto that plot, it can become hard to read and starts to resemble spaghetti.
This is the equivalent of the overplotting problem we previously saw when looking at scatter plots / crossplots. Where a scatter plot fails is when the points on the chart start to lie on top of the same position resulting in a dense indistinguishable blob, but a line chart on the other hand fails when the series cross over each other and it can become difficult to follow them. Even though we can make the chart look good through styling, it now longer supports what the line chart was supposed to do.
To illustrate the problem with line charts, we will work with a set of twenty synthetic daily temperature records throughout a single year.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(11)
days = np.arange(365)
n_stations = 20
def temperature_series(mean_c, amplitude_c, noise_sd=1.8, peak_day=200):
seasonal = mean_c + amplitude_c * np.sin(2 * np.pi * (days - peak_day + 91.25) / 365)
raw = rng.normal(0, noise_sd, days.size + 10)
weather = np.convolve(raw, np.ones(11) / 11, mode=”valid”) * 3.0
return seasonal + weather
stations = {}
for i in range(n_stations):
stations[f”Station {i+1:02d}”] = temperature_series(
mean_c=rng.uniform(4, 19),
amplitude_c=rng.uniform(5, 15),
)
fig, ax = plt.subplots(figsize=(8, 5))
cmap = plt.get_cmap(”tab20”)
for j, k in enumerate(stations):
ax.plot(days, stations[k], color=cmap(j % 20), lw=1.2, label=k)
ax.legend(ncol=2, fontsize=7)
plt.show(When we run the above, we get the following chart.
You can still read a few things from this, such as the overall trend of being cold at both ends of the year and warm in the middle, and you could probably identify the single warmest or coldest line at a given moment.
Beyond that, the data separates from the intended purpose of the line chart. You can’t properly follow Station 12 from January (day 0) to December (day 365), and you constantly have to refer to the legend to remember what line you are tracing, especially if colours are similar.
By having a chart like this, it makes the reader do a lot of hard work to try and extract anything meaningful from it.
Why the Line Chart Fails
The failure in this example has a few separate causes that compound on top of each other:
Lines cross, so a colour you were tracking disappears behind others and re-emerges somewhere you weren’t looking
There are more series than distinct colours the eye can hold apart, so the palette recycles and two stations end up near-identical shades
And the legend sits away from the data, which forces a constant back-and-forth between the key and the plot that gets harder the more entries there are.
None of this can be fixed by looking harder at the plot, and it becomes a case of trying to ask too much of the chart at one time, such as:
follow one specific station
compare two stations
judge the whole population
spot the outlier(s)
It can’t serve all of these purposes on a shared axes once the series count is high. As with overplotting on a crossplot, the plot doesn’t warn you that there is a problem. That is down to you when you are creating it.
More Colours Won’t Save It
The first instinct you may have is to start looking at the colours of the lines and selecting a bigger palette in the hopes that if every station has a unique colour it may help resolve the issue. However, the human eye can only reliably separate a handful of categorical colours at once. Pushing past roughly ten or twelve colours produces shades so close that neighbours start to become indistinguishable from one another.
A twenty-colour or thirty-colour spaghetti plot is essentially a more colourful spaghetti plot, rather than a clearer one. The same goes for changing line styles or markers. These can help a little when we have a handful of series, but they clutter the chart as we add more.
What we need to try and do is to stop demanding that a single chart answer every question we have, but instead we need to pick a plot that answers the questions you have. This may mean several different smaller plots, but each should answer a question you have.
If You Need to See Each Station: Small Multiples
When the task is to inspect the individual behaviour of each series then it is best to give each one its own small panel and keep the axes common across all of them.
This results in small multiples or subplots appearing on the figure (also known as faceting).
fig, axes = plt.subplots(4, 5, figsize=(15, 9), sharex=True, sharey=True)
for ax, k in zip(axes.ravel(), stations):
ax.plot(days, stations[k], color=”#1f3b57”, lw=1.0)
ax.set_title(k, fontsize=9)
plt.tight_layout()
plt.show()With the small multiples, you can still compare across them, which will make identifying a flat, mild year looks from a swinging one at a glance, and an odd station stands out from its neighbours. However, what you may end up losing, is the ability to see everything superimposed, and it comes with a size limit, which can be restrictive with small screen space.
If You Care About One or Two Series: Highlight and Context
Often you do not need to see all of your data series equally, but rather you may want to see how one or two specific stations behave against the rest.
In this case, you would draw the series you care about in strong colour and push everything else into a light grey background, which de-emphasises
fig, ax = plt.subplots(figsize=(8, 5))
for k in stations:
ax.plot(days, stations[k], color=”0.8”, lw=0.9, zorder=1)
ax.plot(days, stations[”Station 07”], color=”#C44E52”, lw=2.4, zorder=3, label=”Station 07”)
ax.plot(days, stations[”Station 14”], color=”#4C72B0”, lw=2.4, zorder=3, label=”Station 14”)
ax.legend()
plt.show()In this example, we will make two distinctive stations show on the plot.
Both of the series we have selected have annual means are almost the same, 10.6 °C and 11.4 °C, which is close enough that they may be missed in a summary table, however, when we look at where the stations are they are nothing alike. Station 07 is maritime: it moves through a range of about 11 °C across the whole year, roughly 7 °C in January and 14 °C in July. Station 14 is continental, swinging through 37 °C, from about −3 °C in January to 28 °C in July.
In January, Station 14 is the 17th warmest of the twenty and Station 07 sits 6th. By July they have switched places almost exactly: Station 14 is 3rd warmest, Station 07 has fallen to 18th. Two stations with the same annual average, ranked at opposite ends of the group depending on when you look. That is the kind of thing that looking an annual mean can hide, but was also completely invisible in the earlier spaghetti plot. The grey context still shows the population that these two series sit within, so you can still have a sense of scale without trying to untangle the lines.
This technique is common in climate charts, where a single year is highlighted against decades of others in grey. It works whenever the question is “how does this one compare to the rest.” It can also easily be applied to any other chart where you want to compare one against a large number of samples.
If You Only Care About the Population: Aggregate
Sometimes the individual identities don’t matter at all and you only want the overall behaviour and its spread. In that case, one option is to summarise by plotting a median line and shade a band between the full dataset’s percentiles.
Y = np.vstack(list(stations.values()))
p10 = np.percentile(Y, 10, axis=0)
p50 = np.percentile(Y, 50, axis=0)
p90 = np.percentile(Y, 90, axis=0)
fig, ax = plt.subplots(figsize=(8, 5))
ax.fill_between(days, p10, p90, alpha=0.25, label=”10th-90th percentile”)
ax.plot(days, p50, lw=2.2, label=”Median”)
ax.legend()
plt.show()This throws away all of the individual station data, and what you get instead is a clean view on the typical seasonal cycle and how much stations vary around it. The band will widen or narrow to show where the population agrees or has minimal spread or where is has maximum spread.
This can be the right plot when you’re characterising a group of measurements rather than any particular member of it. However, it becomes the wrong plot when someone wants to ask something about a specific station, because that information is gone.
The Other Spaghetti: Overlaid Distributions
There is also a second problem where we may have too many distribution lines on a plot that overlap each other.
With three or four groups that reads fine, however, when we start to have ten or twelve, the smooth curves cross and stack into their own kind of spaghetti. This results in you losing the ability to see any single distribution’s shape.
The plot to help alleviate this is the ridgeline plot, which I have spoken about a few times on here. On the ridgeline plot each distribution gets its own baseline, stacked vertically with a little overlap, so the whole set reads like a landscape of ridges (and it also is pretty to look at!).
In this example, we have thirty years of daily temperature at one continental station, split by month, January at the bottom to December at the top.
from scipy.stats import gaussian_kde
month_len = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
edges = np.concatenate([[0], np.cumsum(month_len)])
years = [temperature_series(mean_c=11.0, amplitude_c=15.5) for _ in range(30)]
monthly = [np.concatenate([y[edges[m]:edges[m + 1]] for y in years]) for m in range(12)]
t_grid = np.linspace(-20, 36, 400)
overlap = 1.8
fig, ax = plt.subplots(figsize=(8, 7))
colours = plt.get_cmap(”coolwarm”)(np.linspace(0.05, 0.95, 12))
for i, samples in enumerate(monthly):
dens = gaussian_kde(samples)(t_grid)
dens = dens / dens.max()
base = i * (1.0 / overlap)
ax.fill_between(t_grid, base, base + dens, color=colours[i], alpha=0.85,
zorder=12 - i)
ax.plot(t_grid, base + dens, color=”white”, lw=0.8, zorder=12 - i)
ax.set_yticks([])
ax.set_xlabel(”Temperature (°C)”)
plt.show()The staggered baselines are what make this one work. Each month’s distribution is readable on its own, and because they share a horizontal axis you can still track how the whole seasonal cycle moves, the peaks sliding right through spring into summer and back left again through autumn.
You also can get something from this chart that was not easily identifiable on the line chart and that is that the summer months are visibly narrower than the winter ones, so this station’s winters are not just colder, but are far more variable. If we overlaid each of these on a single axis we would end up with an overlapping mess.
The trade-off with this chart is vertical space and a small amount of occlusion where a tall ridge hides the foot of the one behind it, but for comparing many distributions in an order that means something, it’s far more legible than the overlaid version.
Which One to Reach For
There’s no single replacement for a spaghetti plot, because the spaghetti plot was failing to answer several different questions at once, and each of those questions has its own better chart. Decide what you actually need to see, then pick accordingly: small multiples when you need to read each series, highlight-and-context when one or two matter against the rest, an aggregate band when only the population does, and a ridgeline plot when you’re comparing many distributions rather than many time series. Reaching for more colours is the one move that never helps.
The line chart isn’t wrong, any more than the scatter plot or the histogram was. Line charts are designed to answer the question “how does this series change”, however, when you ask it to answer that same question for twenty series and more at once on the same axes, it becomes painful. It is at this point that you need to stop adding lines and start choosing the plot that fits the question you’re really asking.
The ridgeline above relies on two settings that quietly decide whether the comparison is honest: every curve is normalised to the same peak height, and every KDE is evaluated over one fixed x-range. There is a third setting I left on default, and it is the one most likely to mislead you. The default gaussian_kde bandwidth over-smooths, and it will flatten a genuine right-skew into something that looks symmetric. Over on Substack I’ve written the full build of this chart, from scratch and with joypy, covering bandwidth choice, overlap tuning, and why 4 to 20 groups is the range where a ridgeline actually earns its place.
→ Ridgeline Plots in Matplotlib: An Underused Way to Compare Distributions







