How to Make a Bump Chart in Matplotlib

Make a bump chart in Matplotlib to show how rankings change over time, with smooth curves, highlighting, and top-N entries and exits.

A bump chart shows how rankings change over time, with one line per item. To make one in Matplotlib, turn values into ranks with pandas DataFrame.rank(), plot each column with ax.plot(), and call ax.invert_yaxis() so that first place is at the top.

Quick Example

Six teams collect points over eight weeks. The chart shows each team's league position after every week.

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

rng = np.random.default_rng(4215)
teams = ["Falcons", "Otters", "Comets", "Badgers", "Herons", "Vipers"]
strength = np.array([12, 12, 13, 11, 13, 12])

weekly = rng.poisson(strength, size=(8, len(teams)))
points = pd.DataFrame(weekly, columns=teams,
                      index=[f"Wk {i}" for i in range(1, 9)]).cumsum()
ranks = points.rank(axis=1, ascending=False, method="first").astype(int)

colors = dict(zip(teams, ["#e63946", "#f4a261", "#2a9d8f",
                          "#457b9d", "#8d5fd3", "#6c757d"]))

fig, ax = plt.subplots(figsize=(10, 5.5))
x = np.arange(len(ranks))

for team in teams:
    ax.plot(x, ranks[team], marker="o", markersize=11, linewidth=3,
            color=colors[team])
    ax.annotate(team, (x[-1], ranks[team].iloc[-1]), xytext=(12, 0),
                textcoords="offset points", va="center", fontweight="bold",
                color=colors[team])

ax.invert_yaxis()
ax.set_xticks(x, ranks.index)
ax.set_yticks(range(1, len(teams) + 1))
ax.set_ylabel("League position")
ax.set_title("Standings over eight weeks", loc="left")
ax.spines[["top", "right", "bottom", "left"]].set_visible(False)
ax.tick_params(length=0)
fig.tight_layout()
plt.show()

Bump chart of six teams over eight weeks. The Falcons climb from fifth to first by week 5 and stay there

The labels sit at the end of each line, so readers don't have to match colors against a legend.

Convert Values to Ranks

rank(axis=1, ascending=False) ranks across the columns in each row, giving the highest value rank 1. The data needs one row per time period and one column per item. If yours is in long format, reshape it with df.pivot(index="week", columns="team", values="points").

Ties need a decision:

method= Two items tied for first get
"first" 1 and 2, by order of appearance, so lines never overlap
"min" 1 and 1, and the next gets 3
"dense" 1 and 1, and the next gets 2
"average" (default) 1.5 and 1.5

"first" keeps the chart readable but breaks ties by column order. Here the Falcons and Otters are level on 73 points in week 5, and the Falcons appear first only because their column does. Apply your real tiebreaker, such as goal difference, before plotting if it matters.

Smooth the Lines

Curves make crossings easier to follow. This function connects each pair of ranks with an S-shaped "smoothstep" curve, which stays between the two values, so a line never overshoots into a rank it didn't hold.

def smooth(values, steps=40):
    """Turn points at x = 0, 1, 2... into S-shaped curves that stay level at each one."""
    xs, ys = [], []
    for i, (start, end) in enumerate(zip(values[:-1], values[1:])):
        t = np.linspace(0, 1, steps)
        xs.append(i + t)
        ys.append(start + (end - start) * (3 * t**2 - 2 * t**3))
    return np.concatenate(xs), np.concatenate(ys)


fig, ax = plt.subplots(figsize=(10, 5.5))
for team in teams:
    xs, ys = smooth(ranks[team].to_numpy())
    ax.plot(xs, ys, linewidth=5, color=colors[team], solid_capstyle="round")
    ax.scatter(x, ranks[team], s=420, color=colors[team], zorder=3)
    for xi, rank in zip(x, ranks[team]):
        ax.text(xi, rank, rank, ha="center", va="center", color="white",
                fontsize=9, fontweight="bold", zorder=4)
    ax.text(-0.35, ranks[team].iloc[0], team, ha="right", va="center",
            color=colors[team], fontweight="bold")
    ax.text(x[-1] + 0.35, ranks[team].iloc[-1], team, ha="left", va="center",
            color=colors[team], fontweight="bold")

ax.set_xlim(-1.4, x[-1] + 1.4)
ax.invert_yaxis()
ax.set_xticks(x, ranks.index)
ax.set_yticks([])
ax.spines[:].set_visible(False)
ax.tick_params(length=0)
ax.set_title("Standings with smooth transitions", loc="left")
fig.tight_layout()
plt.show()

Smooth bump chart with the rank number inside each marker and team names at both ends

With the rank inside each marker, the y-axis can go.

Highlight One Line

With more than six or seven lines, color only the one you're discussing and draw the rest in grey:

focus = ranks.iloc[-1].idxmin()

fig, ax = plt.subplots(figsize=(10, 5))
for team in teams:
    is_focus = team == focus
    xs, ys = smooth(ranks[team].to_numpy())
    color = colors[team] if is_focus else "#ced4da"
    ax.plot(xs, ys, linewidth=6 if is_focus else 2.5, color=color,
            zorder=3 if is_focus else 2)
    ax.text(x[-1] + 0.15, ranks[team].iloc[-1], team, va="center",
            color=colors[team] if is_focus else "#6c757d",
            fontweight="bold" if is_focus else "normal")

ax.set_xlim(-0.2, x[-1] + 1)
ax.invert_yaxis()
ax.set_xticks(x, ranks.index)
ax.set_yticks(range(1, len(teams) + 1))
ax.spines[:].set_visible(False)
ax.tick_params(length=0)
ax.set_title(f"How the {focus} reached first place", loc="left")
fig.tight_layout()
plt.show()

Bump chart with the Falcons highlighted in red rising from fifth to first, other teams in grey

Bump Chart or Line Chart?

fig, axes = plt.subplots(1, 2, figsize=(11, 4.2))
for team in teams:
    axes[0].plot(x, points[team], marker="o", color=colors[team], label=team)
    axes[1].plot(x, ranks[team], marker="o", color=colors[team])
axes[0].set_title("Cumulative points")
axes[1].set_title("Rank")
axes[1].invert_yaxis()
for ax in axes:
    ax.set_xticks(x, range(1, 9))
    ax.set_xlabel("Week")
    ax.spines[["top", "right"]].set_visible(False)
axes[0].legend(frameon=False, ncols=2, fontsize=9)
fig.tight_layout()
plt.show()

A line chart of cumulative points, where the lines bunch together, next to a chart of ranks where positions are evenly spaced

The ranks are easy to follow, but they hide the gaps. In the final week one point separates first (109) from second (108). Use a line chart when the size of the difference matters, and a bump chart when the order does. For only two time points, a slope chart shows both.

Bump Chart of a Top N

When items enter and leave a top five, set ranks outside it to NaN. Matplotlib leaves gaps for missing values, so a line starts or stops where the item does.

sales = pd.DataFrame(
    {
        "2021": [90, 84, 77, 65, 52, 31, 24, 12],
        "2022": [88, 91, 70, 60, 66, 45, 30, 20],
        "2023": [75, 95, 68, 49, 80, 72, 33, 41],
        "2024": [70, 93, 55, 40, 96, 85, 62, 58],
        "2025": [64, 89, 48, 36, 104, 99, 78, 60],
    },
    index=["Oat", "Soy", "Almond", "Rice", "Pea", "Coconut", "Cashew", "Hemp"],
)
top_n = 5
all_ranks = sales.rank(ascending=False, method="first")
shown = all_ranks.where(all_ranks <= top_n)  # NaN breaks the line outside the top 5
members = shown.dropna(how="all").index

palette = plt.get_cmap("tab10")
fig, ax = plt.subplots(figsize=(9, 5))
years = np.arange(sales.shape[1])
for i, name in enumerate(members):
    row = shown.loc[name].to_numpy()
    ax.plot(years, row, marker="o", markersize=10, linewidth=3, color=palette(i))
    present = np.flatnonzero(~np.isnan(row))
    first, last = present[0], present[-1]
    ax.annotate(name, (first, row[first]), xytext=(-11, 0), textcoords="offset points",
                ha="right", va="center", color=palette(i), fontweight="bold")
    if last != first:
        ax.annotate(name, (last, row[last]), xytext=(11, 0),
                    textcoords="offset points", va="center", color=palette(i),
                    fontweight="bold")

ax.set_xlim(-0.7, years[-1] + 0.7)
ax.set_ylim(top_n + 0.5, 0.5)
ax.set_xticks(years, sales.columns)
ax.set_yticks(range(1, top_n + 1))
ax.spines[:].set_visible(False)
ax.tick_params(length=0, pad=8)
ax.set_title("Top five plant milks by sales (sample data)", loc="left")
fig.tight_layout()
plt.show()

Top five bump chart from 2021 to 2025. Pea rises from fifth to first, Oat falls from first to fifth, Rice and Almond drop out and Coconut and Cashew enter

Here the years are the columns, so the default axis=0 ranks within each year. An item that leaves and later returns shows a break in its line.

Similar Topics