Savefig with differents names inside a for loop. Python

Im calculating the flow in a lid-driven cavity, and Im plotting the result with a quiver. I want to save the plot in every time step, but obviously, as the name is the same, it´s only keep the last one, how can I do it?

for n in range(nt):

   #Here I do all the calculation to obtain the new u and v

   uC=0.5*(u[:,1:] + u[:,:-1])
   vC=0.5*(v[1:,:] + v[:-1,:])

   plt.cla()
   plt.quiver(x, y, uC, vC);
   plt.draw()
   plt.savefig( "Instant1.png")

So, imagine nt = 10, I want to get ten differents png files. Any ideas? I aprecciate all your help

Upvotes: 1

Views: 5733

Answers (2)

augre
augre

Reputation: 340

You could also do:

plt.savefig("Instand"+str(n)+".png")

Upvotes: 3

Kevin
Kevin

Reputation: 76194

You could change the file name each time:

plt.savefig("Instant{}.png".format(n))

Also, If you have more than ten plots, it might be a good idea to have some leading zeroes, eg. so "Instant5.png" doesn't come after "Instant10.png" in lexicographic order.

plt.savefig("Instant{:03}.png".format(n))

Upvotes: 3

Related Questions