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

时间:2015-11-19 15:49:21

标签: python matplotlib path save

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

我在开始时从Change directory to the directory of a Python script尝试了此命令,但它没有帮助,即使正确提供了文件名:

os.chdir(os.path.dirname(__file__))

1 个答案:

答案 0 :(得分:12)

看起来您可以通过更改默认文件//: Playground - noun: a place where people can play import UIKit protocol ZNumeric { } extension Double: ZNumeric { } class GenericClass<T> { } class RestrictedGenericClass<T:ZNumeric> { } class Container { required init?<T>(type: T.Type) { let a = GenericClass<T>() print(a) if T.self is ZNumeric.Type { print("is numeric") //let b = RestrictedGenericClass<T>() // Will not work obviously //print(b) } } } let cDouble = Container(type: Double.self) // if T.self is ZNumeric.Type is true let cString = Container(type: String.self) // if T.self is ZNumeric.Type is false 来设置此项,请查看 IEEE Std 1003.1-2001 specifies that all-uppercase environment variables are reserved for system use, and variables containing lowercase characters are reserved for applications.下的指南 其中重要的行在savefig参数下:

matplotlibrc

似乎在matplotlib 1.3中引入了http://matplotlib.org/users/customizing.html。我想你可以使用

来设置它
# 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

位于脚本顶部或更改mpl.rcParams["savefig.directory"] = os.chdir(os.path.dirname(__file__)) 文件。我还在使用mpl 1.1.1因此无法测试,抱歉。