[英]How to change default path for "save the figure" in python?
I have a python code to create a figure.我有一个 python 代码来创建一个图形。 After showing it with
plt.show()
, I want to save the figure.用
plt.show()
显示后,我想保存图形。
To avoid messing up the aspect ratio, resolution, etc, I do not want to use the savefig
-command in the code.为了避免弄乱纵横比、分辨率等,我不想在代码中使用
savefig
命令。 Instead, I want to use the "save the figure" button from the figure window.相反,我想使用图形窗口中的“保存图形”按钮。
However, by default, it prompts my home folder as location for the save.但是,默认情况下,它会提示我的主文件夹作为保存位置。 I would like the save to automatically be in the directory where the code was executed.
我希望保存自动位于执行代码的目录中。
How/where can I change this window default path for saving to the current folder (or somewhere else)?如何/在哪里可以更改此窗口默认路径以保存到当前文件夹(或其他地方)?
I tried this command from Change directory to the directory of a Python script at the beginning but it did not help, even though gives the filename correctly:我在开始时尝试将这个命令从Change directory 更改为 Python 脚本的目录,但它没有帮助,即使正确给出了文件名:
os.chdir(os.path.dirname(__file__))
It looks like you may be able to set this by changing the defaults file matplotlibrc
, check out the guidance under http://matplotlib.org/users/customizing.html where the important lines are under the savefig parameters:看起来您可以通过更改默认文件
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
It seems this was introduced in matplotlib 1.3.看来这是在matplotlib 1.3中引入。 I guess you could set this using,
我想你可以设置这个使用,
matplotlib as mpl
mpl.rcParams["savefig.directory"] = os.chdir(os.path.dirname(__file__))
at the top of a script or by changing the matplotlibrc
file.在脚本顶部或通过更改
matplotlibrc
文件。 For the dialog to default to cwd instead of script location (thanks to jjcf89 for this)对话框默认为 cwd 而不是脚本位置(感谢jjcf89 )
matplotlib as mpl
mpl.rcParams["savefig.directory"] = ""
For a non-code solution, you can create a file called matplotlibrc
with the following contents:对于非代码解决方案,您可以创建一个名为
matplotlibrc
的文件,其内容如下:
savefig.directory:
This sets the value of savefig.directory
to the empty string, causing it default to the current working directory.这
savefig.directory
的值savefig.directory
为空字符串,使其默认为当前工作目录。
You can put this matplotlib
file in the current working directory, or in a location specified in the documentation .您可以将此
matplotlib
文件放在当前工作目录中,或者放在文档中指定的位置。 For example, on Linux it should be in .config/matplotlib/
(don't be worried if there's no matplotlibrc
file there already, there wasn't one on my machine either).例如,在 Linux 上,它应该在
.config/matplotlib/
(如果那里没有matplotlibrc
文件,请不要担心,我的机器上也没有)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.