0

I wanted to plot n independent figures by a for loop, with each figure saved to one file. My code is as following:

import matplotlib.pyplot as plt
import numpy as np
for i in range(len(nfile)): #nfile is a list of file names
    data = np.load(nfile[i])
    plt.plot(data[0], data[1])
    plt.savefig("figure_%d.png"%i, dpi=300)

I wanted only the plotting of data[i] to show in figure_i.png, but the former plottings (j=0, ..., i-1) also showed in figure_i.png. Is there any way to solve this?

Thanks a lot!

1
  • 1
    Simply add a plt.figure() at the beginning of your loop, or put a plt.close() at the end. Commented Oct 10, 2016 at 7:03

1 Answer 1

3

At the beginning of your loop, add:

plt.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.