JejeBelfort
JejeBelfort

Reputation: 1663

Specify path of savefig with pylab or matplotlib

I am struggling on how to find the correct way to specify the save path (or repository) when calling the function savefig in matplotlib or pylab.

I tried several syntaxes, but everytime python console returns :

FileNotFoundError: [Errno 2] No such file or directory: '../MyDocs/resource/frames/MyImage.png'

Currently, I have written the following :

pylab.savefig('../MyDocs/resource/frames/MyImage.png')

Does someone know how to do it?

Upvotes: 17

Views: 52089

Answers (2)

黄锐铭
黄锐铭

Reputation: 331

For matplotlib.pyplot, the error FileNotFoundError: [Errno 2] No such file or directory can occur due to nonexistence of the containing folder ../MyDocs/resource/frames/. So maybe first create the folder

import os
os.makedirs('../MyDocs/resource/frames/')

then rerun the savefig function.

Upvotes: 6

T-800
T-800

Reputation: 1603

The tilde operator and the variable $HOME are entered as strings and therefore do not function while saving. You have to provide either relative path (as you did) or provide the full path. Eg. pylab.savefig("/home/username/Desktop/myfig.png").

Upvotes: 11

Related Questions