I have a python code to create a figure. After showing it with plt.show()
, I want to save the figure.
To avoid messing up the aspect ratio, resolution, etc, I do not want to use the savefig
-command in the code. 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:
os.chdir(os.path.dirname(__file__))
Saving a plot on your disk as an image filepyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.
If not provided, defaults to rcParams["figure. figsize"] (default: [6.4, 4.8]) = [6.4, 4.8] .
To get a high-quality image, we can use . eps image format. You can increase the dot per inch value, i.e., dpi. Using savefig() method, we can save the image locally.
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:
# 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. 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.
For the dialog to default to cwd instead of script location (thanks to jjcf89 for this)
matplotlib as mpl
mpl.rcParams["savefig.directory"] = ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With