-3

I am using plt.savefig to save a plot generated by matplotlib.pyplot, and I want to save my figure with different names each time I run the code. How can I do that?

Mamdud
  • 63
  • 5
  • Does this answer your question? [How to save multiple graph in directory with unique file name in python?](https://stackoverflow.com/questions/46913137/how-to-save-multiple-graph-in-directory-with-unique-file-name-in-python) – wovano Mar 29 '22 at 05:43
  • Or see here: [Python: How to create a unique file name?](https://stackoverflow.com/questions/2961509/python-how-to-create-a-unique-file-name) – wovano Mar 29 '22 at 05:43

1 Answers1

0

The easiest would probably be to use the current time, if you don't save multiple files per second.

import datetime

ts = datetime.datetime.now().strftime("%Y%m%d-%H.%M.%S")
plt.savefig(f"figure-{ts}.png")
AKX
  • 152,115
  • 15
  • 115
  • 172