svgtornado
Tornado diagram
Which uncertain inputs move the outcome most when each is varied alone?
How to read it
- Read bars top down: the inputs that move the output most.
- Read each side as the input's low or high value, with others held at base.
- Focus effort on the top bars.
- Callout 1: The most influential input.
- Callout 2: A small swing: this input matters little.
- Callout 3: The base-case output.
Watch out
- It varies one input at a time, so it misses interactions: use a Monte Carlo run for those.
Defaults
- Base-case output
- 1,240
- The output with every input at its base value; bars diverge from here.
- Range -1,000,000,000,000 to 1,000,000,000,000, step 1
Pinned in the code
- Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
- One-way sensitivity: each input moves to its low and high value (e.g. P10 and P90) with every other input at its base case; bars run from the base-case output.
- Interactions between inputs are invisible here — use Sobol indices or Monte Carlo for joint uncertainty.
# Tornado diagram — Codesplash Chart Lab (recipe "tornado")
# controls: base=1240
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("tornado-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
INPUT = "input"
LO = "NPV ($k) at low"
HI = "NPV ($k) at high"
BASE = 1240
d = df.dropna(subset=[INPUT, LO, HI]).assign(swing=lambda t: (t[HI] - t[LO]).abs()).sort_values("swing") # widest on top
base = BASE if BASE is not None else (d[LO].iloc[-1] + d[HI].iloc[-1]) / 2
fig, ax = plt.subplots(figsize=(8, 4.5))
y = np.arange(len(d))
ax.barh(y, d[LO] - base, left=base, color=PALETTE[1], label="input at its low value")
ax.barh(y, d[HI] - base, left=base, color=PALETTE[0], label="input at its high value")
ax.axvline(base, color="black", linewidth=1.2)
ax.set_yticks(y, d[INPUT].astype(str))
ax.set_xlabel("output")
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads tornado-demo.csv · app theme from the optional codesplash helper
# Tornado diagram — Codesplash Chart Lab (recipe "tornado")
# controls: base=1240
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("tornado-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
INPUT = "input"
LO = "NPV ($k) at low"
HI = "NPV ($k) at high"
BASE = 1240
d = df.dropna(subset=[INPUT, LO, HI]).assign(swing=lambda t: (t[HI] - t[LO]).abs()).sort_values("swing")
base = BASE if BASE is not None else (d[LO].iloc[-1] + d[HI].iloc[-1]) / 2
fig = go.Figure()
fig.add_bar(y=d[INPUT].astype(str), x=d[LO] - base, base=base, orientation="h", name="input at its low value", marker_color=PALETTE[1])
fig.add_bar(y=d[INPUT].astype(str), x=d[HI] - base, base=base, orientation="h", name="input at its high value", marker_color=PALETTE[0])
fig.add_vline(x=base, line=dict(color="black", width=1.5))
fig.update_layout(barmode="overlay", xaxis_title="output", yaxis_title=None)
fig.show()Needs pandas, numpy, plotly · reads tornado-demo.csv · app theme from the optional codesplash helper
# Tornado diagram — Codesplash Chart Lab (recipe "tornado")
# controls: base=1240
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("tornado-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["input"]] <- factor(df[["input"]])
input_col <- "input"
lo_col <- "NPV ($k) at low"
hi_col <- "NPV ($k) at high"
base <- 1240
d <- data.frame(input = as.character(df[[input_col]]), lo = df[[lo_col]], hi = df[[hi_col]])
d <- d[stats::complete.cases(d), ]
d$input <- factor(d$input, levels = d$input[order(abs(d$hi - d$lo))]) # widest on top
if (is.null(base)) base <- with(d[which.max(abs(d$hi - d$lo)), ], (lo + hi) / 2)
p <- ggplot(d) +
geom_segment(aes(x = base, xend = lo, y = input, yend = input, colour = "input at its low value"), linewidth = 7) +
geom_segment(aes(x = base, xend = hi, y = input, yend = input, colour = "input at its high value"), linewidth = 7) +
geom_vline(xintercept = base) + scale_colour_manual(values = c(cs_cat[2], cs_cat[1]), name = NULL) +
labs(x = "output", y = NULL) + theme_cs
print(p)Needs ggplot2 · reads tornado-demo.csv · app theme from the optional codesplashr helper