Python的 - matplotlib自动增量保存图片选项

问题描述 投票:0回答:2

我有一个愚蠢的 - 但很烦人 - matplotlib问题。我不断地生成数据,并使用matplotlib弹出图形显示的保存按钮将它们保存到磁盘。要保存使用自动递增的默认名称的指数为文件窗口的默认行为,例如该选项将是拯救“Figure_120.png”的时候就已经有119个保存的其他数据。

但matplotlib的最新版本,默认情况下不这样做,我有编辑和每次我这样做的时候重新命名的数字。难道我做错了什么?而如何在matplotlib常读默认的输出目录的内容要懂得指标的默认值?

enter image description here

python matplotlib save
2个回答
0
投票

您可以保存图中的目录。目录将当前的日期和数字与当前时间进行重命名。相应的代码:

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')  

0
投票

所以托马斯·库恩的建议是有点哈克 - 但它的工作原理:当你点击保存箱 - 这将图保存为标题。我添加了一个随机数,这里的完整的解决方案:

figure = plt.figure()
figure.canvas.set_window_title("Fig_"+str(np.random.randint(1E8)))
© www.soinside.com 2019 - 2024. All rights reserved.