Problem with savefig and usetex

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]:matplotlib.__version__
Out[2]:'0.87.7'

In [3]:import pylab

In [4]:a=[0,1]

In [5]:pylab.plot(a)
Out[5]:[<matplotlib.lines.Line2D instance at 0xb690472c>]

In [6]:pylab.xlabel('M$_\odot$')
Out[6]:<matplotlib.text.Text instance at 0xb6976d2c>

In [7]:pylab.savefig('toto.eps')

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

In [9]:pylab.plot(a)
Out[9]:[<matplotlib.lines.Line2D instance at 0xb692466c>]

In [10]:pylab.xlabel('M$_\odot$')
Out[10]:<matplotlib.text.Text instance at 0xb6916e2c>

In [11]:pylab.savefig('toto.eps')

In [12]:matplotlib.rc('text',usetex=False)

In [13]:pylab.plot(a)
Out[13]:[<matplotlib.lines.Line2D instance at 0xb655cb8c>]

In [14]:pylab.xlabel('10$^3$ M$_\odot$')
Out[14]:<matplotlib.text.Text instance at 0xb65586ac>

In [15]:pylab.savefig('toto.eps')

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

In [17]:pylab.plot(a)
Out[17]:[<matplotlib.lines.Line2D instance at 0xb657286c>]

In [18]:pylab.xlabel('10$^3$ M$_\odot$')
Out[18]:<matplotlib.text.Text instance at 0xb656af6c>

In [19]:pylab.savefig('toto.eps')
sh: line 1: 16796 Erreur de segmentation gs -dBATCH -dNOPAUSE -r6000 -sDEVICE=pswrite -sPAPERSIZE=letter -sOutputFile="/tmp/f71dbe52628a3f83a77ab494817525c6.ps" "/tmp/f71dbe52628a3f83a77ab494817525c6"

···

"/tmp/f71dbe52628a3f83a77ab494817525c6.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)
    812 def savefig(*args, **kwargs):
    813 fig = gcf()
--> 814 return fig.savefig(*args, **kwargs)
    815 if Figure.savefig.__doc__ is not None:
    816 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.

Nicolas

You're example works on my system. But note, you should use raw strings, maybe
that is the source of your problem:

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

···

On Monday 11 December 2006 7:12 am, 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 ?

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 ?

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 ?

Works fine here, with and w/o raw strings.

In [75]: rcParams['text.usetex']=True

In [76]: plot([1,2,3])
Out[76]: [<matplotlib.lines.Line2D instance at 0xa0db754c>]

In [77]: xlabel('10$^3$ M$_\odot$')
Out[77]: <matplotlib.text.Text instance at 0xa0daea2c>

In [78]: savefig('test.eps')

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

In [80]: matplotlib.__revision__
Out[80]: '$Revision: 2835 $'

Maybe you need to upgrade, but that's just a guess.

···

--
cheers,
steve

Random number generation is the art of producing pure gibberish as
quickly as possible.

Steve Schmerler a écrit :

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 ?

Works fine here, with and w/o raw strings.

In [75]: rcParams['text.usetex']=True

In [76]: plot([1,2,3])
Out[76]: [<matplotlib.lines.Line2D instance at 0xa0db754c>]

In [77]: xlabel('10$^3$ M$_\odot$')
Out[77]: <matplotlib.text.Text instance at 0xa0daea2c>

In [78]: savefig('test.eps')

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

In [80]: matplotlib.__revision__
Out[80]: '$Revision: 2835 $'

Maybe you need to upgrade, but that's just a guess.
  

I had matplotlib revision 2835. I made an upgrade. Now I have revision 2905.
I still have problem but it is very strange:
- when I plot [0,1] with xlabel(r'(10$^3$ M$_\odot$)'), it works.
- when I plot [1,2] with xlabel(r'Upper mass for the IMF (10$^3$ M$_\odot$)'), it works.
- but it doesn't work if I plot [0,1] with xlabel(r'Upper mass for the IMF (10$^3$ M$_\odot$)')...

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 0xb67b7e4c>]

In [7]:pylab.xlabel(r'(10$^3$ M$_\odot$)')
Out[7]:<matplotlib.text.Text instance at 0xb691762c>

In [8]:pylab.savefig('toto1.eps')

In [9]:pylab.close()

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

In [11]:pylab.xlabel(r'Upper mass for the IMF (10$^3$ M$_\odot$)')
Out[11]:<matplotlib.text.Text instance at 0xb67d0c6c>

In [12]:pylab.savefig('toto2.eps')

In [13]:pylab.close()

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

In [15]:pylab.xlabel(r'Upper mass for the IMF (10$^3$ M$_\odot$)')
Out[15]:<matplotlib.text.Text instance at 0xb63413ac>

In [16]:pylab.savefig('toto3.eps')
sh: line 1: 19756 Erreur de segmentation gs -dBATCH -dNOPAUSE -r6000 -sDEVICE=pswrite -sPAPERSIZE=letter -sOutputFile="/tmp/11701f1f88db8c691a0ca6ac520c7706.ps" "/tmp/11701f1f88db8c691a0ca6ac520c7706"

···

"/tmp/11701f1f88db8c691a0ca6ac520c7706.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.

Steve Schmerler a écrit :
> 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 ?
>
> Works fine here, with and w/o raw strings.

[...]

I had matplotlib revision 2835. I made an upgrade. Now I have revision
2905. I still have problem but it is very strange:
- when I plot [0,1] with xlabel(r'(10$^3$ M$_\odot$)'), it works.
- when I plot [1,2] with xlabel(r'Upper mass for the IMF (10$^3$
M$_\odot$)'), it works.
- but it doesn't work if I plot [0,1] with xlabel(r'Upper mass for the
IMF (10$^3$ M$_\odot$)')...

It sounds like an issue with one of the dependencies, probably ghostscript. I
have matplotlib-0.87.7, svn revision 2905, and gpl-ghostscript-8.54. I can
not reproduce the problem here.

Darren

···

On Monday 11 December 2006 09:59, Nicolas Champavert wrote:

Darren Dale wrote:

···

On Monday 11 December 2006 09:59, Nicolas Champavert wrote:

Steve Schmerler a �crit :

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 ?

Works fine here, with and w/o raw strings.

[...]

I had matplotlib revision 2835. I made an upgrade. Now I have revision
2905. I still have problem but it is very strange:
- when I plot [0,1] with xlabel(r'(10$^3$ M$_\odot$)'), it works.
- when I plot [1,2] with xlabel(r'Upper mass for the IMF (10$^3$
M$_\odot$)'), it works.
- but it doesn't work if I plot [0,1] with xlabel(r'Upper mass for the
IMF (10$^3$ M$_\odot$)')...

It sounds like an issue with one of the dependencies, probably ghostscript. I have matplotlib-0.87.7, svn revision 2905, and gpl-ghostscript-8.54. I can not reproduce the problem here.

I seem to be running ESP Ghostscript 8.15.3 (this is what 'gs --version' and 'apt-cache show gs' tell me). All the examples work fine here ...

--
cheers,
steve

Random number generation is the art of producing pure gibberish as quickly as possible.

Steve Schmerler a écrit :

Darren Dale wrote:
  

Steve Schmerler a écrit :
      

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 ?
          

Works fine here, with and w/o raw strings.
        

[...]
    

I had matplotlib revision 2835. I made an upgrade. Now I have revision
2905. I still have problem but it is very strange:
- when I plot [0,1] with xlabel(r'(10$^3$ M$_\odot$)'), it works.
- when I plot [1,2] with xlabel(r'Upper mass for the IMF (10$^3$
M$_\odot$)'), it works.
- but it doesn't work if I plot [0,1] with xlabel(r'Upper mass for the
IMF (10$^3$ M$_\odot$)')...
      

It sounds like an issue with one of the dependencies, probably ghostscript. I have matplotlib-0.87.7, svn revision 2905, and gpl-ghostscript-8.54. I can not reproduce the problem here.

I seem to be running ESP Ghostscript 8.15.3 (this is what 'gs --version' and 'apt-cache show gs' tell me). All the examples work fine here ...

I had Ghostscript 8.15.3 too. I have made an update to ghostscript 8.54 and now it works.

Nicolas

···

On Monday 11 December 2006 09:59, Nicolas Champavert wrote: