How to Make a Bullet Chart in Python with Matplotlib

Draw bullet charts with layered Matplotlib bars to compare a value with a target and performance bands, for one KPI or a whole stack.

A bullet chart compares one value with a target against shaded performance bands, in a single narrow bar. Matplotlib has no bullet chart function, but three layers make one: ax.barh() for the bands, a thinner barh() for the value and ax.vlines() for the target. This post uses Matplotlib 3.11.

Quick Example

Quarterly revenue of $276k against a $300k target:

import matplotlib.pyplot as plt
import numpy as np

value, target = 276, 300
bands = [200, 270, 350]                   # upper ends: poor, fair, good
shades = ["#bcbcbc", "#d4d4d4", "#e9e9e9"]

fig, ax = plt.subplots(figsize=(9, 1.8))
left = 0
for upper, shade in zip(bands, shades):
    ax.barh(0, upper - left, left=left, height=0.8, color=shade)
    left = upper

ax.barh(0, value, height=0.3, color="#1d3557")        # actual value
ax.vlines(target, -0.28, 0.28, color="#e63946", lw=3)  # target

ax.set_xlim(0, bands[-1])
ax.set_yticks([0], ["Revenue ($k)"])
ax.spines[:].set_visible(False)
ax.tick_params(length=0)
fig.tight_layout()
plt.show()

Bullet chart showing revenue of 276 thousand dollars as a dark bar ending in the good band, short of a red target line at 300

Read it as: the dark bar is where you are, the red line is where you wanted to be, and the shade under the end of the bar says how good that is.

How do I plot several KPIs?

Put the layers in a function and give each metric its own Axes, so each can have its own units and scale.

def bullet(ax, label, value, target, bands, fmt="{:.0f}",
           shades=("#bcbcbc", "#d4d4d4", "#e9e9e9")):
    left = 0
    for upper, shade in zip(bands, shades):
        ax.barh(0, upper - left, left=left, height=0.8, color=shade)
        left = upper
    ax.barh(0, value, height=0.3, color="#1d3557")
    ax.vlines(target, -0.28, 0.28, color="#e63946", lw=3)
    ax.set_xlim(0, bands[-1])
    ax.set_ylim(-0.45, 0.45)
    ax.set_yticks([])
    ax.xaxis.set_major_formatter(lambda x, pos: fmt.format(x))
    ax.spines[:].set_visible(False)
    ax.tick_params(length=0, labelsize=9)
    ax.text(-0.02, 0.5, label, transform=ax.transAxes, ha="right",
            va="center")


kpis = [
    # label, value, target, band upper ends, tick format
    ("Revenue", 276, 300, [200, 270, 350], "${:.0f}k"),
    ("Gross margin", 61, 58, [45, 55, 75], "{:.0f}%"),
    ("New customers", 410, 520, [350, 480, 600], "{:.0f}"),
    ("Satisfaction", 4.4, 4.5, [3.5, 4.2, 5], "{:.1f}"),
]

fig, axes = plt.subplots(len(kpis), 1, figsize=(9, 4.6))
for ax, row in zip(axes, kpis):
    bullet(ax, *row)
fig.suptitle("Quarterly scorecard", fontweight="bold")
fig.tight_layout()
plt.show()

Four stacked bullet charts for revenue, gross margin, new customers and satisfaction, each with its own scale; only gross margin passes its target

Only gross margin beats its target. Each row has its own axis, so compare rows by band and target, not by bar length.

Lower is better

For costs or response times, put the good shade first and keep the bar starting at zero.

fig, ax = plt.subplots(figsize=(9, 1.8))
bullet(ax, "Response time", value=3.1, target=4, bands=[3, 6, 10],
       fmt="{:.0f} h", shades=("#e9e9e9", "#d4d4d4", "#bcbcbc"))
fig.tight_layout()
plt.show()

Bullet chart of a 3.1 hour response time under a 4 hour target, with the lightest band on the left

Say "lower is better" in the label or a caption. Readers assume the opposite.

Compare teams as percent of target

To compare many rows directly, convert each value to a percentage of its target so they share one axis.

teams = ["Atlas", "Birch Hill", "Cobalt", "Delta", "Evergreen"]
sales = np.array([118, 96, 74, 131, 88])
goals = np.array([100, 110, 100, 120, 80])
pct = sales / goals * 100
order = np.argsort(pct)[::-1]

fig, ax = plt.subplots(figsize=(9, 3.8))
y = np.arange(len(teams))
for upper, shade in [(130, "#e9e9e9"), (100, "#d4d4d4"), (80, "#bcbcbc")]:
    ax.barh(y, upper, height=0.75, color=shade)   # longest drawn first
ax.barh(y, pct[order], height=0.28,
        color=np.where(pct[order] >= 100, "#1d3557", "#e76f51"))
ax.axvline(100, color="#222222", lw=1.5)
for i, value in enumerate(pct[order]):
    ax.text(132, i, f"{value:.0f}%", va="center")
ax.set_yticks(y, np.array(teams)[order])
ax.invert_yaxis()
ax.set_xlim(0, 142)
ax.set_xlabel("Percent of target")
ax.spines[:].set_visible(False)
ax.tick_params(length=0)
fig.tight_layout()
plt.show()

Bullet chart of five teams as percent of target, sorted, with teams above 100 percent in navy and below in orange

Bullet chart or gauge?

A bullet chart does the job of a dial gauge in a fraction of the space, and stacks neatly. For a single large number on a dashboard, Plotly's indicator charts offer both gauges and an interactive bullet style.

Practical tips

  • Use two or three bands, in shades of one color, so they read as background.
  • Start at zero. A bar's length is the value.
  • Keep the same shade order in every row of a scorecard.
  • Explain the bands and the target line in a caption or legend.

Similar Topics