如何在 python 中更改 "save the figure" 的默认路径?
How to change default path for "save the figure" in python?
我有一个 python 代码来创建图形。用plt.show()
显示后,我想保存图形。
为避免弄乱纵横比、分辨率等,我不想在代码中使用 savefig
-命令。相反,我想使用图 window.
中的 "save the figure" 按钮
但是,默认情况下,它会提示我的主文件夹作为保存位置。我希望保存自动在执行代码的目录中。
How/where 我可以更改此 window 默认路径以保存到当前文件夹(或其他位置)吗?
我从 Change directory to the directory of a Python script 开始尝试了这个命令,但它没有帮助,即使给出了正确的文件名:
os.chdir(os.path.dirname(__file__))
您似乎可以通过更改默认文件 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
似乎 this 是在 matplotlib 1.3 中引入的。我想你可以使用
来设置它
matplotlib as mpl
mpl.rcParams["savefig.directory"] = os.chdir(os.path.dirname(__file__))
在脚本顶部或通过更改 matplotlibrc
文件。
对于默认为 cwd 而不是脚本位置的对话框(为此感谢 jjcf89)
matplotlib as mpl
mpl.rcParams["savefig.directory"] = ""
对于非代码解决方案,您可以创建一个名为 matplotlibrc
的文件,其中包含以下内容:
savefig.directory:
这会将 savefig.directory
的值设置为空字符串,使其默认为当前工作目录。
您可以将此 matplotlib
文件放在当前工作目录中,或放在 documentation 中指定的位置。例如,在 Linux 上,它应该在 .config/matplotlib/
中(如果那里已经没有 matplotlibrc
文件,请不要担心,我的机器上也没有)。
我有一个 python 代码来创建图形。用plt.show()
显示后,我想保存图形。
为避免弄乱纵横比、分辨率等,我不想在代码中使用 savefig
-命令。相反,我想使用图 window.
中的 "save the figure" 按钮
但是,默认情况下,它会提示我的主文件夹作为保存位置。我希望保存自动在执行代码的目录中。
How/where 我可以更改此 window 默认路径以保存到当前文件夹(或其他位置)吗?
我从 Change directory to the directory of a Python script 开始尝试了这个命令,但它没有帮助,即使给出了正确的文件名:
os.chdir(os.path.dirname(__file__))
您似乎可以通过更改默认文件 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
似乎 this 是在 matplotlib 1.3 中引入的。我想你可以使用
来设置它matplotlib as mpl
mpl.rcParams["savefig.directory"] = os.chdir(os.path.dirname(__file__))
在脚本顶部或通过更改 matplotlibrc
文件。
对于默认为 cwd 而不是脚本位置的对话框(为此感谢 jjcf89)
matplotlib as mpl
mpl.rcParams["savefig.directory"] = ""
对于非代码解决方案,您可以创建一个名为 matplotlibrc
的文件,其中包含以下内容:
savefig.directory:
这会将 savefig.directory
的值设置为空字符串,使其默认为当前工作目录。
您可以将此 matplotlib
文件放在当前工作目录中,或放在 documentation 中指定的位置。例如,在 Linux 上,它应该在 .config/matplotlib/
中(如果那里已经没有 matplotlibrc
文件,请不要担心,我的机器上也没有)。