17

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__))
Community
  • 1
  • 1
physiker
  • 889
  • 3
  • 16
  • 30

2 Answers2

19

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"] = ""
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • great! now it works. I could not fix it by editing the matplotrc but then your last command helped, with the small change that I have to put: ... = "path-to-my-folder". somehow the os.chdir did not work. PS: really good to have a look at matplotrc setting. Now I can change many other things as well – physiker Nov 20 '15 at 09:52
  • Hi @physiker, glad to hear it worked. I wonder if it is even possible to set dynamic file paths of the form you wanted in the `matplotrc` file. If it's simply run like a script when matplotlib is imported, I assume you could `import os` and use something like `os.chdir`. In a script, the command `os.chdir(os.path.dirname(__file__))` should return a string which would be as valid as a hardwired "path-to-my-folder", unless I've missed something... – Ed Smith Nov 20 '15 at 12:35
  • Hi @Ed Smith , actually "path-to-my-folder" was just " . " which is naively dynamic! I will try more with your suggestion and if I manage, i will report back. Thanks – physiker Nov 20 '15 at 13:23
  • 1
    it's important to note that `savefig.directory` is only used when clicking on the `Save` button in the interactive figure, but NOT in the script `plt.savefig('/some/path')` – Ciprian Tomoiagă Apr 11 '17 at 00:43
  • 3
    Note that the following works if you want the dialog to default to cwd instead of script location. ```# Configure save dialog to use current directory``` ```import matplotlib as mpl``` ```mpl.rcParams["savefig.directory"] = ""``` – jjcf89 Sep 06 '17 at 14:05
  • When plotting with multiprocessing, be careful to update the parameter in the proper process. Took me some time to figure out why it did not work... – Andris Jun 14 '22 at 13:53
0

For a non-code solution, you can create a file called matplotlibrc with the following contents:

savefig.directory:

This sets the value of savefig.directory to the empty string, causing it default to the current working directory.

You can put this matplotlib file in the current working directory, or in a location specified in the documentation. 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).

Jack M
  • 4,769
  • 6
  • 43
  • 67