How to clear memory completely of all Matplotlib plots

后端 未结 3 596
粉色の甜心
粉色の甜心 2020-12-05 17:50

I have data analysis module that contains functions which call on Matplotlib pyplot API multiple times to generate up to 30 figures in each run. These figures get immediatel

3条回答
  •  眼角桃花
    2020-12-05 18:41

    I have data analysis module that contains functions which call on Matplotlib pyplot API multiple

    Can you edit your functions which is calling matplotlib? I was facing the same issue, I tried following command but none of it worked.

    plt.close(fig)
    fig.clf()
    gc.collect()
    %reset_selective -f fig
    

    Then one trick worked for me, instead of creating a new figure every time, I pass the same fig object to the function and this solved my issue.

    for example use,

    fig = plt.figure()
    for i in range(100):
        plt.plot(x,y)
    

    instead of,

    for i in range(100):
        fig = plt.figure()
        plt.plot(x,y)
    

提交回复
热议问题