SHAP splits each prediction into one contribution per feature, and the shap.plots module draws those contributions. The workflow is explainer = shap.TreeExplainer(model), then shap_values = explainer(X), then a plot such as shap.plots.beeswarm(shap_values) for the global picture or shap.plots.waterfall(shap_values[i]) for one row. The code here uses shap 0.54 and scikit-learn 1.9.
Quick Example
The data is a synthetic food-delivery set, so it runs offline. The model predicts delivery time in minutes from distance, traffic, order size, rain, and courier experience.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import shap
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(11)
n = 1500
df = pd.DataFrame({
"distance_km": rng.gamma(3, 1.3, n).round(1),
"traffic_index": rng.uniform(0, 10, n).round(1),
"items": rng.integers(1, 12, n),
"rain_mm": rng.exponential(1.2, n).round(1),
"courier_years": rng.integers(0, 9, n),
})
df["delivery_min"] = (
8
+ 3.2 * df["distance_km"]
+ 0.9 * df["traffic_index"] * (df["distance_km"] / 4)
+ 0.7 * df["items"]
+ 1.5 * df["rain_mm"].clip(upper=6)
- 0.8 * df["courier_years"]
+ rng.normal(0, 2.5, n)
).round(1)
X = df.drop(columns="delivery_min")
y = df["delivery_min"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=11)
model = GradientBoostingRegressor(n_estimators=300, max_depth=3, learning_rate=0.05,
random_state=11).fit(X_train, y_train)
explainer = shap.TreeExplainer(model)
shap_values = explainer(X_test)
shap.plots.beeswarm(shap_values, show=False)
plt.title("What drives predicted delivery time")

Each dot is one order. Its horizontal position is how many minutes that feature added to (right) or took off (left) the prediction, and its color is the feature's value. Long distances (red) push delivery time up by as much as 44 minutes. Experienced couriers (red on the courier_years row) land on the left, so they shave time off. The model scores R² = 0.93 on the test set, so these contributions describe real structure in this data.
Pass show=False when you want to add a title or call plt.savefig() afterward. Without it, shap shows and closes the figure immediately.
What SHAP Values Mean
For a regression model, a SHAP value is in the units of the target, here minutes. For one row, the SHAP values plus the baseline (the average prediction, 28.1 minutes) add up exactly to the model's prediction. That additivity is what makes the waterfall plot work, and you can check it:
np.abs(shap_values.values.sum(axis=1) + shap_values.base_values - model.predict(X_test)).max()
# ~1e-13
shap_values is an Explanation object with .values (one row per sample, one column per feature), .base_values, and .data (the original feature values). For tree models, TreeExplainer computes exact values and is fast. For models that aren't trees, shap.Explainer(model.predict, X_train) picks a slower model-agnostic method.
Bar Plot: Global Feature Importance
shap.plots.bar(shap_values, show=False)

The bar length is the mean absolute SHAP value, the average number of minutes a feature moves a prediction in either direction. distance_km averages 8.06 minutes, about four times traffic_index (2.09). Use the bar plot for a quick ranking and the beeswarm when you need to see direction and spread, since a bar can't say whether high values help or hurt.
Waterfall Plot: Explain One Prediction
i = int(np.argmin(np.abs(model.predict(X_test) - 45))) # an order predicted near 45 minutes
shap.plots.waterfall(shap_values[i], show=False)

The plot starts at the baseline E[f(X)] = 28.113 and adds each feature's contribution until it reaches the prediction f(x) = 45.131. For this order, 7.3 km adds 15.22 minutes, heavy traffic adds 2.86, and a courier with six years of experience subtracts 1.72. Gray numbers on the left are the feature values. Use this plot to answer "why did the model predict that for this row?" in a support ticket or a review.
Index shap_values[i] with a position, not a DataFrame label. Features are sorted by absolute contribution, and max_display= collapses the smallest ones into a single bar when you have many columns.
Dependence Plot: How One Feature Affects Predictions
shap.plots.scatter(shap_values[:, "distance_km"], color=shap_values[:, "traffic_index"],
show=False)

shap.plots.scatter is the dependence plot. The x-axis is the feature's value, the y-axis is its SHAP value, and the gray histogram at the bottom shows where most of the data sits. Here distance has a nearly straight effect of about 4.2 minutes per kilometer (a line fit through the points). Coloring by a second feature reveals interactions. At long distances the red, high-traffic points sit above the blue ones, because the data was built so that traffic costs more minutes on longer trips. Leave out color= and shap picks the feature it thinks interacts most.
In older shap releases this function was shap.dependence_plot(...). The shap.plots.* functions take the Explanation object directly and are the current API.
Which SHAP Plot Should I Use?
| Question | Plot | Function |
|---|---|---|
| Which features matter most overall? | Bar | shap.plots.bar |
| Which features matter, and does high or low push the prediction up? | Beeswarm | shap.plots.beeswarm |
| Why did the model predict this value for one row? | Waterfall | shap.plots.waterfall |
| What does the model do as one feature changes? | Dependence | shap.plots.scatter |
Practical Tips
- Use a sample on big data. SHAP on a tree model is fast, but beeswarm plots get dense past a few thousand points. Explain a random sample of 1,000 to 2,000 rows.
- Explain held-out data. Computing SHAP values on the test set shows how the model behaves on rows it didn't train on.
- Classifiers return per-class values. For multiclass models,
shap_valueshas a third dimension, so select a class first withshap_values[:, :, k]. For binary models, check which output scale you got (log-odds for many tree models) before reading the numbers as probabilities. - SHAP describes the model, not the world. A large contribution says the model uses a feature heavily, not that changing it would change the real outcome. Correlated features also share credit in ways that can be hard to read.
For a simpler ranking without SHAP, scikit-learn's permutation importance is a good cross-check.