How to completely clear memory from all Matplotlib charts

I have a data analysis module that contains functions that invoke the MPplotlib API several times to generate up to 30 digits in each run. These numbers are immediately written to disk after they are created, so I need to clear them from memory. Currently, at the end of each of my functions, I am doing

import matplotlib.pyplot as plt plt.clf() 

however, I'm not sure if this statement can really clear the memory. I am especially worried because I see that every time I run my module for debugging, free memory space is reduced. Can someone tell me what I need to do to really clear my memory every time I write graphics to disk?

Thanks.

+16
source share
2 answers

Especially when you start several processes or threads, it is much better to define the figure variable and work with it directly:

 from matplotlib import pyplot as plt f = plt.figure() f.clear() plt.close(f) 

In any case, you should combine the use of plt.clear () and plt.close ()

+5
source

I have a data analysis module that contains functions that call the Matplotlib Pyplot API several

Can you edit your functions calling matplotlib? I ran into the same problem, I tried to execute the following command, but none of this worked.

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

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

for example use

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

instead

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

Source: https://habr.com/ru/post/1214293/


All Articles