blalterman
blalterman

Reputation: 575

matplotlib savefig.directory in style file not working

I am using a style file to control the default behavior of my figure saving and have set the following in save-article.mplstyle under the directory /Users/me/.matplotlib/stylelib.

savefig.directory   : ./Plots/ 

When I only use the current directory ., I get the expected output with something like plt.savefig("test.pdf"). When I use ./Plots/, the file does not appear. I can provide my full save-article.mplstyle file if that is helpful. The Plots directory currently exists and I can save files to it using commands like plt.savefig("./Plots/test.pdf"). When I check rcParams after loading the style files, the correct information is populated in the file.

How can I fix this?

Upvotes: 2

Views: 3288

Answers (1)

kikocorreoso
kikocorreoso

Reputation: 4219

As stated in the documentation:

#savefig.directory   : ~        # default directory in savefig dialog box,
                                # leave empty to always use current working directory

This option is used when you work in interactive mode and when you click in the save icon of the interactive mode it will use the directory defined in savefig.directory option.

Your best bet would be to use os.path or pathlib to define the directory you want to use:

import os

basepath = os.path.abspath('Plots')

# [...other code...]

plt.savefig(os.path.join(basepath, 'figurename.pdf))

Upvotes: 3

Related Questions