如何在python中更改"保存图"的默认路径?

phy*_*ker 10 python path matplotlib save

我有一个python代码来创建一个数字.在展示之后plt.show(),我想保存这个数字.
为了避免弄乱纵横比,分辨率等,我不想savefig在代码中使用-command.相反,我想使用图窗口中的"保存图"按钮.
但是,默认情况下,它会提示我的主文件夹作为保存位置.我希望save能自动存在于执行代码的目录中.
如何/在哪里可以更改此窗口默认路径以保存到当前文件夹(或其他位置)?

我在开始时尝试将此命令从 Change目录更改为Python脚本的目录,但它没有帮助,即使正确提供了文件名:

os.chdir(os.path.dirname(__file__))
Run Code Online (Sandbox Code Playgroud)

Ed *_*ith 12

看起来您可以通过更改默认文件来设置它matplotlibrc,查看http://matplotlib.org/users/customizing.html 下的重要行位于savefig参数下的指南:

# the default savefig params can be different from the display params

...

savefig.directory   : ~        # default directory in savefig dialog box, 
                               # leave empty to always use current working directory
Run Code Online (Sandbox Code Playgroud)

看来是在matplotlib 1.3中引入.我想你可以设置这个,

mpl.rcParams["savefig.directory"] = os.chdir(os.path.dirname(__file__))
Run Code Online (Sandbox Code Playgroud)

在脚本的顶部或通过更改matplotlibrc文件.我还在使用mpl 1.1.1因此无法测试,抱歉.

  • 请注意,如果希望对话框默认为cwd而不是脚本位置,则可以使用以下命令。#配置保存对话框以使用当前目录。 (3认同)