0

I am trying to save a figure. Something quite simple.

The program that makes the plots receives a list called "data"

data['Reading [V/dec]','OK values', 'Defects', 'Valids', 'Non valids']

fig.savefig(data[0]+'.png')

If I use the code above, the figure is just not saved.

If I do like this:

data['Reading [V per dec]','OK values', 'Defects', 'Valids', 'Non valids']

fig.savefig(data[0]+'.png')

The figure is saved.

So I am wondering why I cannot use 'Reading [V/dec]' as a name for my .png file. And how to make it take the name as it is 'Reading [V/dec]'.

Just one more comment, according to an aswer I got, I have to say that I am using Windows.

codeKiller
  • 5,493
  • 17
  • 60
  • 115

1 Answers1

1

This is nothing to do with matplotlib really, and is instead because of how operating systems handle directories. / is often used for separating directories, i.e. my file is stored here /path/to/my/file.py. As such, it is unlikely that you're allowed to use the symbol / in a filename as it would confuse matters far too much.

This is definitely not allowed in Windows and Ubuntu, not sure about OS X or other Linux distros but I suspect it is not allowed for all.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • No. Go and try to create a text file called `my/text/file.txt`. It will fail. – Ffisegydd Nov 18 '14 at 13:46
  • 1
    On Windows the filename cannot have any of the following symbols `\/:*?"<>|`. – Ffisegydd Nov 18 '14 at 13:47
  • 1
    See also http://stackoverflow.com/questions/9847288/is-it-possible-to-use-in-a-filename –  Nov 18 '14 at 13:50
  • 1
    IIRC forward slashes are valid for filenames (on both *nix and M$), they will be interpreted as path separators though. And in most situation the directory path would have to exist in the first place to not raise an IOError. –  Nov 18 '14 at 13:50
  • When I tried to make a file using `vi` it told me that the directory did not exist, which suggests you are correct. – Ffisegydd Nov 18 '14 at 13:52