Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 526
JejeBelfort Avatar asked Apr 01 '15 14:04

JejeBelfort


2 Answers

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.

like image 126
黄锐铭 Avatar answered Oct 27 '22 02:10

黄锐铭


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").

like image 38
T-800 Avatar answered Oct 27 '22 01:10

T-800