I have a silly - but very annoying - matplotlib question. I constantly generate figures and save them to disk using the save button on the matplotlib pop-up figure display. The default behaviour of that window used to auto-increment the index of the default name for the file to be saved, eg the option would be to save "Figure_120.png" when there were already 119 other figures saved.
But latest version of matplotlib doesn't do this by default and I have to edit and rename figures every time I do this. Am I doing something wrong? And how was matplotlib always reading the content of the default output directory to know how to index the default value?
You can save Figure in a directory. Directory will be renamed by the current date and figure with current time. Corresponding code:
import os.path
import os, errno
cur_Date = time.strftime("%Y-%m-%d")
cur_Time = time.strftime("%H-%M")
%create directory if it did not exist
try:
os.makedirs(cur_Date)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# your figure creation lines ....
# save the figure to file
fig.savefig(cur_Time+'.png')
So Thomas Kuhn's suggestion is a bit hacky - but it works: when you click on save box - it will save the figure as in title. I added a random number, here's the full solution:
figure = plt.figure()
figure.canvas.set_window_title("Fig_"+str(np.random.randint(1E8)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.