Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default path for "save the figure" in python?

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__))
like image 497
physiker Avatar asked Nov 19 '15 15:11

physiker


People also ask

How do I save a specific figure in Python?

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.

What is the default figure size in Python?

If not provided, defaults to rcParams["figure. figsize"] (default: [6.4, 4.8]) = [6.4, 4.8] .

How do I save a figure in high resolution Python?

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.


1 Answers

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"] = ""
like image 68
Ed Smith Avatar answered Sep 21 '22 07:09

Ed Smith