Can´t save matplotlib figure to .eps in Windows while using text.usetex : True

自闭症网瘾萝莉.ら 提交于 2019-12-24 17:27:47

问题


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.



来源:https://stackoverflow.com/questions/33524976/can%c2%b4t-save-matplotlib-figure-to-eps-in-windows-while-using-text-usetex-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!