How to save a plot (generated by shap_values) to png?
Asked Answered
C

1

10

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?

Camarilla answered 17/9, 2019 at 14:50 Comment(0)
C
20

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')
Camarilla answered 18/9, 2019 at 6:36 Comment(1)
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.Cube

© 2022 - 2024 — McMap. All rights reserved.