Force Plot Colors

The scatter and beeswarm plots create Python matplotlib plots that can be customized at will. However, the force plots generate plots in Javascript, which are harder to modify inside a notebook. In the case that the colors of the force plot want to be modified, the plot_cmap parameter can be used to change the force plot colors.

[1]:
import xgboost
from IPython.display import display

import shap

# load JS visualization code to notebook
shap.initjs()

# train XGBoost model
X, y = shap.datasets.california(n_points=1_000)
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)

# explain the model's predictions using SHAP values
explainer = shap.TreeExplainer(bst)
explanation = explainer(X)
[2]:
# visualize the first prediction's explanation with default colors
shap.plots.force(explanation[0])
[2]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.

The different color map names are available in the color-set.js file. Supported cmaps are shown below.

To add one, modify the file with a color map name, and a list containing the two colors of the color map, the first one being the one for positive SHAP values, and the second one for the negative SHAP values.

[3]:
# visualize the first prediction's explanation with the supported colormaps
for cmap in (
    "RdBu",
    "GnPR",
    "CyPU",
    "PkYg",
    "DrDb",
    "LpLb",
    "YlDp",
    "OrId",
):
    print(f"{cmap=}")
    display(shap.plots.force(explanation[0], plot_cmap=cmap))
cmap='RdBu'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
cmap='GnPR'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
cmap='CyPU'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
cmap='PkYg'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
cmap='DrDb'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
cmap='LpLb'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
cmap='YlDp'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.
cmap='OrId'
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.

The same can be applied to lists of explanations.

[4]:
# visualize the first 5 predictions explanations with a dark red dark blue color map
shap.plots.force(explanation[0:5], plot_cmap="DrDb")
[4]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.

You can also pass a list of colors as the value for the plot_cmap parameter to render the force plot with those colors. Those colors must be hex-coded.

[5]:
# visualize the first 5 predictions explanations with a custom color map
shap.plots.force(explanation[0:5], plot_cmap=["#FF5733", "#335BFF"])
[5]:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must also trust this notebook (File -> Trust notebook). If you are viewing this notebook on github the Javascript has been stripped for security. If you are using JupyterLab this error is because a JupyterLab extension has not yet been written.