Problem with savefig and usetex

No the '\' is not the problem. \odot is a LaTeX command. I still have the problem if I don't use it (see below with only pylab.xlabel(r'Upper mass for the IMF (10$^3$)')) and \\odot writes 'odot' and doesn't write the 'odot' symbol.

Gary Ruben a écrit :

I haven't tried it, but my guess is the '\' character is the problem.

> pylab.xlabel('10$^3$ M$_\odot$')

Try
pylab.xlabel(r'10$^3$ M$_\odot$')
             ^
             Add raw string marker.

or maybe
pylab.xlabel('10$^3$ M$_\\odot$')

Gary R.

Nicolas Champavert wrote:

Hello,

  I have some problems when trying to save a figure with usetex=True. Sometimes, it is not possible to save the figure when trying to put an xlabel with LaTeX inside.
It works with pylab.xlabel('M$_\odot$') but not with pylab.xlabel('10$^3$ M$_\odot$') (see below). Do you know why ?

In [1]:import matplotlib

In [2]:import pylab

In [3]:matplotlib.__version__
Out[3]:'0.87.7'

In [4]:matplotlib.__revision__
Out[4]:'Revision: 2905 '

In [5]:matplotlib.rc('text',usetex=True)

In [6]:pylab.plot([0,1])
Out[6]:[<matplotlib.lines.Line2D instance at 0xb67dde4c>]

In [7]:pylab.xlabel(r'Upper mass for the IMF (10$^3$)')
Out[7]:<matplotlib.text.Text instance at 0xb693d62c>

In [8]:pylab.savefig('test.eps')
sh: line 1: 19944 Erreur de segmentation gs -dBATCH -dNOPAUSE -r6000 -sDEVICE=pswrite -sPAPERSIZE=letter -sOutputFile="/tmp/098f6bcd4621d373cade4e832627b4f6.ps" "/tmp/098f6bcd4621d373cade4e832627b4f6"

···

"/tmp/098f6bcd4621d373cade4e832627b4f6.output"

---------------------------------------------------------------------------
exceptions.RuntimeError Traceback (most recent call last)

/home/champavert/<ipython console>

/usr/local/lib/python2.4/site-packages/matplotlib/pylab.py in savefig(*args, **kwargs)
    811 def savefig(*args, **kwargs):
    812 fig = gcf()
--> 813 return fig.savefig(*args, **kwargs)
    814 if Figure.savefig.__doc__ is not None:
    815 savefig.__doc__ = _shift_string(Figure.savefig.__doc__)

/usr/local/lib/python2.4/site-packages/matplotlib/figure.py in savefig(self, *args, **kwargs)
    685 kwargs[key] = rcParams['savefig.%s'%key]
    686
--> 687 self.canvas.print_figure(*args, **kwargs)
    688
    689 def colorbar(self, mappable, cax=None, **kw):

/usr/local/lib/python2.4/site-packages/matplotlib/backends/backend_tkagg.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, **kwargs)
    184 if dpi is None: dpi = rcParams['savefig.dpi']
    185 agg = self.switch_backends(FigureCanvasAgg)
--> 186 agg.print_figure(filename, dpi, facecolor, edgecolor, orientation,
    187 **kwargs)
    188 self.figure.set_canvas(self)

/usr/local/lib/python2.4/site-packages/matplotlib/backends/backend_agg.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, **kwargs)
    486 ps = self.switch_backends(FigureCanvasPS)
    487 ps.print_figure(filename, dpi, facecolor, edgecolor,
--> 488 orientation, **kwargs)
    489 elif ext.find('pdf')>=0:
    490 from backend_pdf import FigureCanvasPdf

/usr/local/lib/python2.4/site-packages/matplotlib/backends/backend_ps.py in print_figure(self, outfile, dpi, facecolor, edgecolor, orientation, papertype)
   1009 # Let's keep the usetex stuff seperate from the generic postscript
   1010 self._print_figure_tex(outfile, dpi, facecolor, edgecolor,
-> 1011 orientation, papertype)
   1012 else:
   1013 if isinstance(outfile, file):

/usr/local/lib/python2.4/site-packages/matplotlib/backends/backend_ps.py in _print_figure_tex(self, outfile, dpi, facecolor, edgecolor, orientation, papertype)
   1243 elif rcParams['text.usetex']:
   1244 if False: pass # for debugging
-> 1245 else: gs_distill(tmpfile, ext=='.eps', ptype=papertype, bbox=bbox)
   1246
   1247 if isinstance(outfile, file):

/usr/local/lib/python2.4/site-packages/matplotlib/backends/backend_ps.py in gs_distill(tmpfile, eps, ptype, bbox)
   1341 exit_status = os.system(command)
   1342 fh = file(outfile)
-> 1343 if exit_status: raise RuntimeError('ghostscript was not able to process \
   1344 your image.\nHere is the full report generated by ghostscript:\n\n' + fh.read())
   1345 else: verbose.report(fh.read(), 'debug')

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

ESP Ghostscript 8.15 (2006-04-19)
Copyright (C) 2004 artofcode LLC, Benicia, CA. All rights reserved.
This software comes with NO WARRANTY: see the file COPYING for details.