0

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.

  • 1
    You 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 Answers1

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