Memory overflow while saving Matplotlib plots in a loop

I use an iterative loop to build data using Matplotlib. When the code has saved about 768 charts, it throws the following exception.

RuntimeError: Could not allocate memory for image 

My computer has about 3.5 GB of RAM. Is there a way to free memory in parallel so that memory is not exhausted?

+4
source share
2 answers

Do you remember close your numbers when you are done with them? eg:.

 import matplotlib.pyplot as plt #generate figure here #... plt.close(fig) #release resources associated with fig 
+7
source

As a slightly different answer, remember that you can reuse numbers. Sort of:

 fig = plt.figure() ax = plt.gca() im = ax.imshow(data_list[0],...) for new_data in data_list: im.set_cdata(new_data) fig.savefig(..) 

This will lead to the fact that your code will work much faster, since it will not need to set and reset the digit more than 700 times.

+3
source

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


All Articles