7

I use Shap library to visualize variable importance.

I try to save shap_summary_plot as 'png' image but my image.png but them get an empty image

this is the code that I have used:

shap_values = shap.TreeExplainer(modelo).shap_values(X_train)
shap.summary_plot(shap_values, X_train, plot_type="bar")
plt.savefig('grafico.png')

The code worked but the image saved was empty.

How can I save the plot as image.png?

Maite89
  • 273
  • 2
  • 8

1 Answers1

16

I solved the problem. The code is:

shap_values = shap.TreeExplainer(modelo).shap_values(X_train)
shap.summary_plot(shap_values, X_train, plot_type="bar",show=False)
plt.savefig('grafic.png')
Maite89
  • 273
  • 2
  • 8
  • Side note: make sure you clear/close previous figures if you make multiple plots. Otherwise in my case, they're all plotted overlapping each other. – Moobie Jun 14 '23 at 08:39