Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving figures in a loop

I don't know if this is possible and I hope to acurally portray what I wish to accomplish.

Say we have:

from pylab import *
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(10,10))

y = 2

for x in xrange(0,5):
    value = [1,int(y)]
    plt.plot(value)
    plt.savefig("value" + y + ".png")
    y+=1

So my goal here is to get 5 plots (or I think this would give me 6 plots), but I want them to each save with different names so they don't overwrite each time it goes through the loop. Is this possible?

Note: The numbers and values in this example are arbitrary. My hope is just to plot in a loop like this and have a dynamic method to save the name of the figure.

like image 359
Plato's Student Avatar asked May 06 '26 21:05

Plato's Student


1 Answers

You don't need y, just use the x in your loop.

for x in xrange(1,6): # starts at 1 and goes to 5
    print ("value" + str(x) + ".png")
value1.png
value2.png
value3.png
value4.png
value5.png
like image 161
Padraic Cunningham Avatar answered May 08 '26 13:05

Padraic Cunningham