Black Lives Matter. Please consider donating to Black Girls Code today.

Return/Render Shap plots in div

I want to return shap plots on the callback of button click which uses drop down values as input :

Following is the code to build shap :

model_shap = RandomForestRegressor(random_state=37,max_depth= 11, max_features= ‘log2’, n_estimators= 100)[s_final_v1],filtered_target)
shap_values = shap.TreeExplainer(model_shap).shap_values(model_data[s_final_v1])
shap.summary_plot(shap_values, model_data[s_final_v1],show=False)

Can anyone suggest how to return this to div on a button click ?

Hi @Mighty welcome to the forum! From a quick look at the shap repo, it looks like shap.summary_plot returns a matplotlib figure object, which you can save to a png or svg file with

shap.summary_plot(shap_values, model_data[s_final_v1],show=False)
import matplotlib.pyplot as plt

and then you can display the image inside a div with html.Image from dash_html_components.

To be honest ,I did exactly same

shap.summary_plot(shap_values, filtered_model_data[s_final_v1],show=False)

Its not saving + dash automatically takes me to the first tab of the screen ( and this is placed on the third tab )

It doesn’t let me see the error as it redirects to first tab. However , I see a warning which I have not been able to decode :

WARNING: QApplication was not created in the main() thread.