11

When displaying summary_plot, the color bar does not show.

shap.summary_plot(shap_values, X_train)

Summary plot

I have tried changing plot_size. When the plot is higher the color bar appears, but it is very small - doesn't look like it should.

shap.summary_plot(shap_values, X_train, plot_size=0.7)

Higher summary plot

Here is an example of a proper looking color bar. Proper summary plot

Does anyone know if this can be fixed somehow?

How to reproduce:

import pandas as pd
import shap
import sklearn
from sklearn.ensemble import RandomForestRegressor

# a classic housing price dataset
X,y = shap.datasets.boston()

# a simple linear model
model = RandomForestRegressor(max_depth=6, random_state=0, n_estimators=10)
model.fit(X, y)
shap_values = shap.TreeExplainer(model).shap_values(X)
shap.summary_plot(shap_values, X)

In this case, the color bar is displayed, but it is very small. I have chosen such an example to make it easy to retrieve the data.

JohanC
  • 71,591
  • 8
  • 33
  • 66
nietoperz21
  • 303
  • 3
  • 12
  • I am using shap==0.40.0 and matplotlib==3.5.1. I have added an example code in the question, where the color bar is displayed, but it is very small. – nietoperz21 Dec 23 '21 at 14:04
  • Thank you for the example code. I can't reproduce your problem with matplotlib 3.4.3. But maybe also the Python version and the backend are important, as suggested in the [linked post](https://stackoverflow.com/questions/70294681/colormap-bar-on-shap-summary-plot-not-displaying-properly)? Perhaps somebody at [Shap's github](https://github.com/slundberg/shap/issues) can help you out? – JohanC Dec 23 '21 at 15:32
  • In [shap's source code](https://github.com/slundberg/shap/blob/master/shap/plots/_beeswarm.py#L364) there is a call to `pl.colorbar(..., aspect=1000)`. That doesn't seem a realistic value. Maybe that works different in the new matplotlib version? Could you try something like `import matplotlib.pyplot as plt; plt.gcf().axes[-1].set_aspect(30)` to try to change that aspect ratio? – JohanC Dec 23 '21 at 16:10
  • I'm having the exact same issue! My shap and matplotlib versions are also the same. I didn't even realize the colorbar was there until I saved the image. It's razor thin. – ethan tenison Dec 26 '21 at 20:52

5 Answers5

5

I had the same problem as you did, and I found that the solution was to downgrade matplotlib to 3.4.3.. It appears SHAP isn't optimized for matplotlib 3.5.1 yet.

ethan tenison
  • 393
  • 3
  • 14
2

As mentioned above, it seems that the handling of colorbar or box aspect ratio has been changed in matplotlib.pyplot version 3.5. However, you can correct that.

  • Use shap.summary_plot(..., show=False) to allow altering the plot
  • As mentioned above, set the aspect of the colorbar with plt.gcf().axes[-1].set_aspect(1000)
  • Then set also the aspect of the color bar's box plt.gcf().axes[-1].set_box_aspect(1000)

This gives you the old result back. If you want to make the colorbar thicker, set the aspect to 100.

2

I had a similar issue and I was using max_display=10. The below code solved my problem:

plt.gcf().axes[-1].set_aspect(100)
plt.gcf().axes[-1].set_box_aspect(100)
1

just use 'auto' aspect to do the trick:

plt.gcf().axes[-1].set_aspect('auto')
plt.tight_layout()
# As mentioned, smaller "box_aspect" value to make colorbar thicker
plt.gcf().axes[-1].set_box_aspect(50) 

shap version 0.40.0; matplotlib version 3.5.1

HairyCat
  • 54
  • 1
0

Calling plt.colorbar() explicitly will do:

import xgboost
import shap

X, y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)

explainer = shap.Explainer(model, X)
shap_values = explainer(X)

shap.plots.beeswarm(shap_values, show=False, color_bar=False)
plt.colorbar()
plt.show()

enter image description here

Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72
  • Hey Sergey, this didn't work for me with summary_plot, and I also have no colour gradient on the plot itself - any ideas? – JED HK Nov 17 '22 at 11:30