
SHAP (SHapley Additive exPlanations) is a popular approach to model explainability. The shap
Python package enables you to quickly create a variety of different plots out of the box. Its distinctive blue and magenta colors make the plots immediately recognizable as SHAP plots.
Unfortunately, the Python package default color palette is neither colorblind- nor photocopy-safe. For this reason, you might want to change the SHAP plot’s colors. Or maybe you just want to adjust the colors to your corporate identity. Or maybe the only thing you want is to add a title.
For some SHAP plots customization is easier than for others. Some plotting functions already provide such parameters, but you have to get crafty for some plot types.
For some SHAP plots customization is easier than for others.
In this article, we will discuss how to
- Customizing Attributes of Figure and Axis Objects, such as adjusting the figure size, adding titles and labels, and using subplots.
- Customizing Colors for summary plots, waterfall plots, bar plots, and force plots.
In this article, we will focus on customizing the SHAP plots. If you need an introduction or a refresher on how to use the SHAP package, I recommend this article by Conor O’Sullivan:
Make sure you have SHAP version 0.41.0 installed. Otherwise, you will run into errors with this article’s code snippets [1, 3].
import shap
print(shap.__version__)
If not, upgrade the package as follows:
!pip install --upgrade shap
Customizing Attributes of Figure and Axis Objects
Before we get into any coloring, let’s cover the basics first. In this section, we will discuss how to customize the attributes of figure and axis objects.
To be able to modify the SHAP plots, you need to set the show
parameter to False
.
show
: Whether matplotlib.pyplot.show() is called before returning. Setting this to False allows the plot to be customized further after it has been created. [2]
Adjusting Figure Size
Unfortunately, adjusting the figsize
parameter doesn’t help much when you want to adjust the figure size of your SHAP plot. Instead, you need to set show = False
and then you can adjust the size of the figure as shown below:
fig = plt.figure()
shap.plots.bar(shap_values[0], show = False)
plt.gcf().set_size_inches(20,6)
plt.show()
Adjusting Title, Labels, and Limits
When you have set show = False
, you can freely adjust the figure’s attributes as usual. Below you can see an example of how to change the title, x-label, and x-limit.
fig = plt.figure()
shap.plots.bar(shap_values[0], show = False)
plt.title("Custom Title")
plt.xlabel("Custom X-Label")
plt.xlim([-12,12])
plt.show()
Using Subplots
If you want to display multiple subplots in a figure, you can do so by using the method .add_subplot()
.
fig = plt.figure()
ax0 = fig.add_subplot(131)
shap.plots.bar(shap_values[0], show = False)
ax1 = fig.add_subplot(132)
shap.plots.bar(shap_values[1], show = False)
ax2 = fig.add_subplot(133)
shap.plots.bar(shap_values[2], show = False)
plt.gcf().set_size_inches(20,6)
plt.tight_layout()
plt.show()
Customizing Colors
Before we get into how to customize the colors of the plots, let’s take a little detour and understand why the SHAP default color palette is unfavorable.
Recently, I wrote an article stressing the importance of colorblind-safe data visualizations.
Unfortunately, the SHAP default color palette is neither colorblind- nor photocopy-safe (at the time of writing in August 2022) as you can see in the following image.

Although the edge colors – magenta (#ff0051) and blue (#008bfb) – are colorblind-safe, they are not photocopy-safe since they have similar hues.
Additionally, the gradient color palette is not colorblind-safe because the purple tones in the middle range are not distinguishable from the blue tones in the low range for people with red-green colorblindness.
Summary Plot
Note: For this section, you must have installed at least SHAP version 0.40.0 installed [1].
For the summary plot, it’s a piece of cake to change the color palette. Since version 0.40.0 you can simply use the cmap
parameter [1].
shap.summary_plot(shap_values,
X_train,
cmap = "plasma")

Waterfall Plot
Note: For this section, you must have installed at least SHAP version 0.41.0 [3].
Since we don’t have a parameter available for the Waterfall plot, we need to get a little crafty.
First, you need to set the show
parameter to False
to be able to modify the plot [2]. For the waterfall plot, we need to adjust the colors for the FancyArrow and the Text objects.
import matplotlib
import matplotlib.pyplot as plt
# Default SHAP colors
default_pos_color = "#ff0051"
default_neg_color = "#008bfb"
# Custom colors
positive_color = "#ca0020"
negative_color = "#92c5de"
# Plot Waterfall Plot
shap.plots.waterfall(shap_values[0], show = False)
# Change the colormap of the artists
for fc in plt.gcf().get_children():
for fcc in fc.get_children():
if (isinstance(fcc, matplotlib.patches.FancyArrow)):
if (matplotlib.colors.to_hex(fcc.get_facecolor()) == default_pos_color):
fcc.set_facecolor(positive_color)
elif (matplotlib.colors.to_hex(fcc.get_facecolor()) == default_neg_color):
fcc.set_color(negative_color)
elif (isinstance(fcc, plt.Text)):
if (matplotlib.colors.to_hex(fcc.get_color()) == default_pos_color):
fcc.set_color(positive_color)
elif (matplotlib.colors.to_hex(fcc.get_color()) == default_neg_color):
fcc.set_color(negative_color)
plt.show()

Bar Plot
For the bar plot we need to take a similar approach to the waterfall plot.
Again, you need to set the show
parameter to False
to be able to modify the plot [2]. For the bar plot, we need to adjust the colors for the Rectangle and the Text objects.
# Plot bar plot
shap.plots.bar(shap_values[0], show = False)
# Change the colormap of the artists
for fc in plt.gcf().get_children():
# Ignore last Rectangle
for fcc in fc.get_children()[:-1]:
if (isinstance(fcc, matplotlib.patches.Rectangle)):
if (matplotlib.colors.to_hex(fcc.get_facecolor()) == default_pos_color):
fcc.set_facecolor(positive_color)
elif (matplotlib.colors.to_hex(fcc.get_facecolor()) == default_neg_color):
fcc.set_color(negative_color)
elif (isinstance(fcc, plt.Text)):
if (matplotlib.colors.to_hex(fcc.get_color()) == default_pos_color):
fcc.set_color(positive_color)
elif (matplotlib.colors.to_hex(fcc.get_color()) == default_neg_color):
fcc.set_color(negative_color)
plt.show()

Force Plot
Luckily, for the force plot we can simply use the plot_cmap
parameter.
You can either specify the colors manually:
# Custom colors
positive_color = "#ca0020"
negative_color = "#92c5de"
shap.force_plot(shap_values[0],
plot_cmap = [positive_color, negative_color])

Or use a predefined color palette:
shap.force_plot(shap_values[0],
plot_cmap = "PkYg")

Conclusion
This article showcased how to quickly customize SHAP plots. While it is easy for some plots, we have to get crafty for others.
Generally, you need to use the show = False
parameter to adjust the SHAP plots.
Then you can easily customize Figure and Axis objects’ attributes like the figure size, titles, and labels, or you can add subplots.
To customize colors
- For some plot types, we can directly use the available parameters. E.g., for the summary plot, we can use the
cmap
parameter and for the force plot we can use theplot_cmap
parameter. - For some plot types, we don’t have a parameter for changing the colors available. Therefore, we need to change the colors of the artists one by one for plots like waterfall plots and bar plots.
If you run into problems with this article’s code snippets, make sure to check whether you have at least SHAP version 0.41.0 installed.
Enjoyed This Story?
Subscribe for free to get notified when I publish a new story.
Find me on LinkedIn, Twitter, and Kaggle!
References
[1] C. S. "Changing the gradient color of shap.summary_plot()
to specific 2 or 3 RGB gradient palette Colors". stackoverflow.com. https://stackoverflow.com/questions/60153036/changing-the-gradient-color-of-shap-summary-plot-to-specific-2-or-3-rgb-grad (accessed August 8, 2022)
[2] "SHAP", "Welcome to the SHAP documentation". shap.readthedocs.io. https://shap.readthedocs.io/en/latest/index.html (accessed August 8, 2022)
[3] J. Stevenson, "Fix bug in waterfall with show=False". github.com. https://github.com/slundberg/shap/pull/2342 (accessed August 8, 2022)