I am running a loop to extract data and graph plots using Seaborn, Pandas and Python. I just want to save each plot as a graphic and close it but I am not able to figure out how to do this.
/usr/local/lib/python3.6/dist-packages/seaborn/axisgrid.py:311: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_open_warning).
I had expected g.close() to work but I got the error: AttributeError: 'FacetGrid' object has no attribute 'close'
for o in options:
s = "SELECT * from options_yahoo where contract_name = '" + o + "'
SQL_Query = pd.read_sql_query(s, conn)
df = pd.DataFrame(SQL_Query)
g = sns.relplot( kind="line", data=df[['bid','ask','lastprice']])
g.savefig( o+ ".png")
g.close()
I expect to be able to have a more efficient solution that doesn't use up so much memory and bring warning errors. Some best practices would be much appreciated.
g.fig.close()- busybearg.fig.close()results in anAttributeError: 'Figure' object has no attribute 'close'. Useplt.close(g.fig)instead, see my answer below. - Johan