'How to add title to the plot of shap.plots.force with Matplotlib?
I want to add some modifications to my force plot (created by shap.plots.force
) using Matplotlib, e.g. adding title, using tight layout etc. However, I tried to add title and the title doesn't show up. Any ideas why and how can I add the title using Matplotlib?
import numpy as np
import shap
import matplotlib.pyplot as plt
myBaseline=1.5
shap_values_0 = np.array([-1, -4, 3])
test_point_0 = np.array([11, 12, 13])
features_names = ['a1','a2','a3']
shap.plots.force(myBaseline,shap_values_0,test_point_0,features_names,matplotlib = 1)
plt.suptitle("This is my title") # It doesn't show up, why?
fig = plt.gcf()
fig.canvas.draw()
plt.close()
Solution 1:[1]
The last lines in force_plot
are:
if show:
plt.show()
else:
return plt.gcf()
so, if you set show = False
you can get prepared SHAP plot as figure
object and customize it to your needs as usual:
import shap
myBaseline = 1.5
shap_values_0 = np.array([-1, -4, 3])
test_point_0 = np.array([11, 12, 13])
features_names = ["a1", "a2", "a3"]
shap.plots.force(
myBaseline, shap_values_0, test_point_0, features_names, matplotlib=True, show=False
)
plt.title("This is my title", y=1.75)
plt.show()
Solution 2:[2]
I had to add show=0
at shap.plots.force
, i.e.
shap.plots.force(myBaseline,shap_values_0,test_point_0,features_names,matplotlib = 1, show=0)
I have no idea why it works, but it does.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sergey Bushmanov |
Solution 2 | desertnaut |