streamlit_freecodecamp icon indicating copy to clipboard operation
streamlit_freecodecamp copied to clipboard

boston-house-ml-app.py - PyplotGlobalUseWarning: You are calling st.pyplot() without any arguments.

Open datalifenyc opened this issue 4 years ago • 1 comments

When plotting the 2 SHAP figures, the figures appear, but the following warning also appears.

st.header('Feature Importance')
plt.title('Feature importance based on SHAP values')
shap.summary_plot(shap_values, X)
st.pyplot(bbox_inches = 'tight')
st.write('----')

plt.title('Feature importance based on SHAP values (Bar)')
shap.summary_plot(shap_values, X, plot_type = 'bar')
st.pyplot(bbox_inches = 'tight')

PyplotGlobalUseWarning: You are calling st.pyplot() without any arguments. After December 1st, 2020, we will remove the ability to do this as it requires the use of Matplotlib's global figure object, which is not thread-safe.

The temporary solution is to configure the streamlit option:

st.set_option('deprecation.showPyplotGlobalUse', False)

However, is there a solution that does not require disabling deprecation warnings?

For some of the prior examples, like the basketball_app.py, it was easy to determine what to pass as the fig:

st.pyplot(f)

For this streamlit dashboard, which incorporates the SHAP library, what can be passed in as the <fig> in line 81 & 86: st.pyplot(<fig?>, bbox_inches = 'tight')?

Version Reference: python - 3.10.0 matplotlib - 3.5.1 streamlit - 1.5.1 scikit-learn - 1.0.2 shap - 0.39.0

datalifenyc avatar Mar 06 '22 16:03 datalifenyc

Correct you need to add fig st.pyplot(fig, bbox_inches='tight') with fig, ax = plt.subplots(figsize=(15,5)). Further details here https://discuss.streamlit.io/t/pyplotglobalusewarning/6578/5

cyrbaufr avatar Jun 05 '22 18:06 cyrbaufr