Can´t save matplotlib figure to .eps in Windows wh

2019-08-11 14:06发布

问题:

When I run the following code: http://dpaste.com/0210P09 everything works fine on my Linux distro.

However, in Windows7 (64bit) I cannot successfully save it in eps or svg.

This is the error that I get:

Traceback (most recent call last):

  File "<ipython-input-1-b8da411d11b0>", line 1, in <module>
    runfile('C:/Users/12151056/Documents/Python Scripts/MyPython/1Tplot_2.py', wdir='C:/Users/12151056/Documents/Python Scripts/MyPython')

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "C:/Users/12151056/Documents/Python Scripts/MyPython/1Tplot_2.py", line 67, in <module>
    plt.savefig('excel-6.eps')

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 577, in savefig
    res = fig.savefig(*args, **kwargs)

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1476, in savefig
    self.canvas.print_figure(*args, **kwargs)

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2211, in print_figure
    **kwargs)

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1009, in print_eps
    return self._print_ps(outfile, 'eps', *args, **kwargs)

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1033, in _print_ps
    **kwargs)

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1398, in _print_figure_tex
    rotated=psfrag_rotated)

  File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1556, in gs_distill
    your image.\nHere is the full report generated by ghostscript:\n\n' + fh.read())

TypeError: Can't convert 'bytes' object to str implicitly

If i use text.usetex : False I have no problem at all.

I have livetex full and Ghostscript installed.

Any suggestions to try and solve it?

回答1:

There are two problems here. The first and minor one is that in

C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-package\matplotlib\backends\backend_ps.py", line 1556

you should change

fh.read()

to

fh.read().decode()

This is a Python 2/3 bug, that occurs while handling the fact that something with Ghostscript went wrong.

The main problem is that matplotlib cannot find Ghostscript because it is not in the path environmental variable. You can check that by opening a command prompt and trying the command gswin32c or gs. If these commands are not found you have to add the path of these Ghostscript executables to the path environmental variable. In my case it was:

C:\Program Files (x86)\gs\gs8.54\bin

but it depends on your installation path.