简体   繁体   English

Python-Matplotlib自动增量保存图形选项

[英]Python - matplotlib autoincrement save figure option

I have a silly - but very annoying - matplotlib question. 我有一个愚蠢的-但很烦人-matplotlib问题。 I constantly generate figures and save them to disk using the save button on the matplotlib pop-up figure display. 我不断生成图形,并使用matplotlib弹出图形显示屏上的保存按钮将其保存到磁盘。 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. 该窗口的默认行为是用于自动增加要保存文件的默认名称的索引,例如,当已经保存了119个其他图形时,该选项将是保存“ Figure_120.png”。

But latest version of matplotlib doesn't do this by default and I have to edit and rename figures every time I do this. 但是默认情况下,最新版本的matplotlib不会执行此操作,因此每次执行此操作时,我都必须编辑和重命名图形。 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? matplotlib如何始终读取默认输出目录的内容以了解如何索引默认值?

在此处输入图片说明

You can save Figure in a directory. 您可以将Figure保存在目录中。 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. 因此,托马斯·库恩(Thomas Kuhn)的建议有点不客气-但它的确有效:当您单击保存框时,它会将图形保存为标题。 I added a random number, here's the full solution: 我添加了一个随机数,这是完整的解决方案:

figure = plt.figure()
figure.canvas.set_window_title("Fig_"+str(np.random.randint(1E8)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM