简体   繁体   中英

Error creating .eps output with matplotlib with LaTex text rendering (usetex)

I was trying to create .eps output with Latex text rendering using the example provided by the matplotlib homepage .

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This demo is tex_demo.py modified to have unicode. See that file for
more information.
"""
from __future__ import unicode_literals
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['text.usetex']=True
mpl.rcParams['text.latex.unicode']=True
from numpy import arange, cos, pi
from matplotlib.pyplot import (figure, axes, plot, xlabel, ylabel, title,
     grid, savefig, show)

figure(1, figsize=(6,4))
ax = axes([0.1, 0.1, 0.8, 0.7])
t = arange(0.0, 1.0+0.01, 0.01)
s = cos(2*2*pi*t)+2
plot(t, s)

xlabel(r'\textbf{time (s)}')
ylabel(r'\textit{Velocity (\u00B0/sec)}', fontsize=16)
title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
      fontsize=16, color='r')
grid(True)
savefig('tex_demo.eps')

Where I just added the " savefig('tex_demo.eps') " command at the end. However, I get the error message:

File "", line 1, in
runfile('E:/Software/Python/Test/Plot/Matplotlib/tex_unicode_demo.py', wdir='E:/Software/Python/Test/Plot/Matplotlib')

File "C:\\Python27\\lib\\site-packages\\spyderlib\\widgets\\externalshell\\sitecustomize.py", line 540, in runfile execfile(filename, namespace)

File "E:/Software/Python/Test/Plot/Matplotlib/tex_unicode_demo.py", line 27, in savefig('tex_demo.eps',format='eps')

File "C:\\Python27\\lib\\site-packages\\matplotlib\\pyplot.py", line 577, in savefig res = fig.savefig(*args, **kwargs)

File "C:\\Python27\\lib\\site-packages\\matplotlib\\figure.py", line 1470, in savefig self.canvas.print_figure(*args, **kwargs)

File "C:\\Python27\\lib\\site-packages\\matplotlib\\backends\\backend_qt5agg.py", line 161, in print_figure FigureCanvasAgg.print_figure(self, *args, **kwargs)

File "C:\\Python27\\lib\\site-packages\\matplotlib\\backend_bases.py", line 2194, in print_figure **kwargs)

File "C:\\Python27\\lib\\site-packages\\matplotlib\\backends\\backend_ps.py", line 992, in print_eps return self._print_ps(outfile, 'eps', *args, **kwargs)

File "C:\\Python27\\lib\\site-packages\\matplotlib\\backends\\backend_ps.py", line 1016, in _print_ps **kwargs)

File "C:\\Python27\\lib\\site-packages\\matplotlib\\backends\\backend_ps.py", line 1381, in _print_figure_tex rotated=psfrag_rotated)

File "C:\\Python27\\lib\\site-packages\\matplotlib\\backends\\backend_ps.py", line 1539, in gs_distill your image.\\nHere is the full report generated by ghostscript:\\n\\n' + fh.read())

RuntimeError: ghostscript was not able to process your image. Here is the full report generated by ghostscript:

I already made sure that the pathes to the executeables of LaTeX, dvipng and Ghostscript are in my path variable.

Matplotlib: 1.4.2

Ghostscript: 9.15

Python: 2.7.6

OS: Windows 8.1

I haven't managed to directly solve your problem. However I have found a work around. I have an install of Cygwin on my windows PC. Before I call my python script I added the Cygwin bin directory into my windows path:

set "PATH=%PATH%;C:\cygwin64\bin"

For me at least this solved my problem. Hope it is of help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM