Add the function to close the figure in a loop