I’m needing to iterate over a data set with around 50000 rows and I need to generate and save one image (graphic) each 24 rows. to save one image, I’m using the function plt.savefig() but I don’t know if I can use this function to save all this images. Because I will need change the name of the figure in each iteration, and I don´t know how to do that. Will be almost 2090 images. I tried to do this in a few ways but maybe I’m using this function in a wrong way.
Asked
Active
Viewed 664 times
0
-
1You can simply insert the variable used for iteration (e.g. row number) in your file name. E.g., if your loop is `for i in range(50000)`, you can simply write `plt.savefig(f'filename_{i}.png')` or something similar. – luuk Mar 19 '21 at 21:18
-
Thanks! I did exactly this way and now is working! But now I have one more question... If I want to save all these images directly on my computer, specifying the path, this is possible? – Daniel Carlos Mar 20 '21 at 00:43
-
From your other comment, I see that you are using Google Colab. I am not familiar with Colab, but I think you should be able to follow the steps described in [the answers to this question.](https://stackoverflow.com/questions/50453428/how-do-i-download-multiple-files-or-an-entire-folder-from-google-colab) – luuk Mar 20 '21 at 12:07
1 Answers
0
From your question, it sounds like you want to increment the names of each file so that all 2090 images save. You can use this code to change the image name, by passing in some int n
. This will add leading zeros so the images sort correctly.
plt.savefig("image{:04}.jpeg".format(n))

imLightSpeed
- 147
- 1
- 9
-
Now is working, but if I want to save all these images directly on my computer, specifying the path, this is possible? – Daniel Carlos Mar 20 '21 at 00:44
-
For a subdirectory it is as simple as adding the path in front the file name /subfolder/image0001.jpeg – imLightSpeed Mar 20 '21 at 01:04
-
I tried to do that but doesn't work. I'm using Google Colab, is this the problem? – Daniel Carlos Mar 20 '21 at 05:08