7

enter image description here

Is there any way that I can change the shap plot background color or text color in the dark theme? I need either the white background or white text.

The plot is an object of IPython.core.display.HTML.

It is generated by

shap.force_plot(explainer.expected_value[1], shap_values[1][0,:], X_test.iloc[0,:],link="logit")

Thanks for the help!

J.D
  • 1,885
  • 4
  • 11
  • 19

3 Answers3

5

After adding the argument of matplotlib=True, the problem is solved.

shap.force_plot(explainer.expected_value[1], shap_values[1][0,:], X_test.iloc[0,:],link="logit", matplotlib=True)

It seems the plot is created with matplotlib on a html file.

To plot properly, firstly I used style.use('seaborn-dark') in the dark model of Jupyter notebook.

J.D
  • 1,885
  • 4
  • 11
  • 19
  • 3
    This works (thanks for the style tip!), however: you'll lose plot interactivity, won't you? Furthermore: `"matplotlib = True is not yet supported for force plots with multiple samples!"` - which means you can't use it when inspecting several predictions at once... – Daddy32 Jun 03 '20 at 12:40
1

A workaround is to export the figure as an html file, and open that in the browser:

p = shap.force_plot(explainer.expected_value[1], shap_values[1][0,:], X_test.iloc[0,:],link="logit")
shap.save_html('my_force_plot.html', p)

But I guess a light color scheme might also circumvent the problem?

AllanLRH
  • 1,124
  • 1
  • 12
  • 23
0

I've found solution on Shap's Github (Dark mode problem). Try little "HTML correction" from Ipython.display. I've added 2 lines to your code:

force_plot = shap.force_plot(explainer.expected_value[1], shap_values[1][0,:], X_test.iloc[0,:],link="logit")

from IPython.display import HTML
HTML(f"<div style='background-color:Lavender;'>{shap.getjs() + force_plot.html()}</div>")
Vegarus
  • 101
  • 1
  • 3
  • Could you provide some context with this answer? As it is currently written, it may be difficult for people to understand how this answers the question. – Shawn Hemelstrand May 23 '23 at 09:26