@st.cache_resource matplotlib figure vanishes after rerun


Using the code below, the matplotlib fig vanishes every time streamlit reruns if I leave the parameter the same.
If I change the parameter of the fig, it shows up again.

The function is inside a class.

Steps to reproduce

Code snippet:

class FigureView:
    #some code

    def risk_analysis_1_dot(_self, probability:str, impact:str, title:str):
        #some code
        return plt

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:
Fig matplotlib fig shows even if parameter were not chanched

Actual behavior:
.matplotlib fig vanishes if other values get updated but not matplotlib fig input data

Debug info

  • Streamlit version: streamlit==1.21.0
  • Python version: 3.10
  • Using local venv

That code is syntactically incorrect even if you put actual code in place of the comments.

Are you passing the returned plt to st.pyplot, outside of the function? Or, are you using st.pyplot inside of the risk_analysis_1_dot? If the former, it should work fine. If the latter, it will only show the output the first time you run the code.

After your edit I was able to add some code to your class and use it to plot something, but I couldn’t reproduce the issue, the plot never vanishes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.