0

I want to create a lot, at least 10000 plot, so I use for loop to do it, Here is my code.

for i in range(0, 10000):
    fig, ax = plt.subplots(1, 1, num=0)
    x = np.linspace(0, 100, 100)
    y = np.sin(x)
    
    ax.plot(x, y)
    plt.tight_layout()
    fig.savefig("test.png")
    plt.close(fig=0)
    
def plot_test():
    for i in range(0, 10000):
        fig, ax = plt.subplots(1, 1, num=0)
        x = np.linspace(0, 100, 100)
        y = np.sin(x)
        
        ax.plot(x, y)
        plt.tight_layout()
        fig.savefig("test.png")
        plt.close(fig=0)

No matter use plt.close() in function or outside, The memory is still increasing, I have checked plt.close("all") and plt.close(fig) are not working too. Hope anyone can tell me how to use plt.close() properly.

  • Does this help? https://stackoverflow.com/a/55834853/6366770 – David Erickson Jan 19 '21 at 01:33
  • Thank you, however the method in that post are not work for me. –  Jan 19 '21 at 01:48
  • Given that established methods do not work for you, you may want to provide more information on the environment in which you run (unsuccessfully) your code (jupyter, spyder, you name it). – Mr. T Jan 19 '21 at 05:59
  • After I tried for several methods, I found the memory increasing may be due to I run the code in spyder IDE, when I run in a system terminal, there is no significant memory increasing, even I create new `fig` in every loop. –  Jan 20 '21 at 02:31

1 Answers1

0

So I believe plt.close() is working fine. Your problem is that every plot has the same name. This overwrites any existing file in the directory. Essentially, it creates test.png and immediately overwrites it with the every iteration. This solution adds i to the end of the plot name and saves to a directory I made called test. With this many plots it takes a little bit to finish the iteration

import numpy as np
import matplotlib
# The default backend is "tkinter" which is used for displaying the graphs. 
# "agg" is more lightweight and avoids a tkinter error when making lots of plots
matplotlib.use('agg')
import matplotlib.pyplot as plt
import gc



for i in range(0, 10000):
    # This just shows the progress of the loop
    print(i)
   
    fig, ax = plt.subplots(1, 1, num=0)
    x = np.linspace(0, 100, 100)
    y = np.sin(x)

    ax.plot(x, y)
    plt.tight_layout()
    fig.savefig("test/test{}.png".format(str(i)))
    plt.close(fig=fig)
    gc.collect()
BeanBagTheCat
  • 435
  • 4
  • 7
  • Thank you for your reply, however it does not solve the problem. The problem here is the fig was created, but plt.close() did not close it, therefore in every iteration, a new fig was created again, the memory was taken by fig, I have tried to create fig outside the loop, and in every loop, use plt.cla() to clear ax, it works for me, however, there is a error message : UserWarning: Tight layout not applied. The bottom and top margins cannot be made large enough to accommodate all axes decorations. –  Jan 19 '21 at 02:29
  • Are you sure `fig` is taking up that memory? The python garbage collector gets rid of it on each iteration. `plt.close` wouldn't affect this behavior. https://stackoverflow.com/questions/36729249/does-python-garbage-collect-at-the-end-of-an-iteration-in-a-loop – BeanBagTheCat Jan 19 '21 at 02:32
  • I am not sure whether fig or other things take the memory, but when I use your code to run, nothing changed, the memory is still increasing, however when I put fig outside the loop, the memory will not increase, therefore, my opinion is that fig took the memory. Have you tried your code? –  Jan 19 '21 at 03:02
  • Try using `gc.collect()` at the end. Ill update my code for you. Personally I don't see any significant memory increase on my end for either implementation. – BeanBagTheCat Jan 19 '21 at 03:11
  • Thank you for your reply, however, I found `gc` does not work for me. Till now, put `fig` outside the loop may be a temporary solution to me. –  Jan 19 '21 at 03:30
  • Alright, best of luck! Sorry I couldn't be more helpful. – BeanBagTheCat Jan 19 '21 at 03:31
  • After I tried for several methods, I found the memory increasing may be due to I run the code in spyder IDE, when I run in a system terminal, there is no significant memory increasing, even I create new `fig` in every loop. –  Jan 20 '21 at 02:31