dataprofessor / streamlit_freecodecamp

Build 12 Data Apps in Python with Streamlit
577 stars 541 forks source link

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

Open datalifenyc opened 2 years ago

datalifenyc commented 2 years ago

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

cyrbaufr commented 2 years ago

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