Skip to content

savefig to vector graphics with LaTeX fails with certain characters #10272

Closed
@miromarszal

Description

@miromarszal

Saving vector graphic figures with certain characters in LaTeX will fail. Here I tried to use the \textmu character. Could it be related to #8068?

Code for reproduction

Here I'm saving to PostScript. It gives an error that I think is the most informative and points to some problems with ghostscript. Outputs for SVG and PDF below.

import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylabel(r'\textmu')
plt.savefig('fig.ps')

Output - PS

Traceback (most recent call last):
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1520, in gs_distill
    report = subprocess.check_output(command, stderr=subprocess.STDOUT)
  File "/usr/lib/python3.6/subprocess.py", line 336, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.6/subprocess.py", line 418, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['gs', '-dBATCH', '-dNOPAUSE', '-r6000', '-sDEVICE=ps2write', '-sPAPERSIZE=letter', '-sOutputFile=/tmp/tmpt8w23igm.ps', '/tmp/tmpt8w23igm']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Untitled.py", line 8, in <module>
    plt.savefig('fig.ps')
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 701, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1834, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2267, in print_figure
    **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 910, in print_ps
    return self._print_ps(outfile, 'ps', *args, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 937, in _print_ps
    **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1359, in _print_figure_tex
    rotated=psfrag_rotated)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1525, in gs_distill
    '\n\n' % exc.output.decode("utf-8")))
RuntimeError: ghostscript was not able to process your image.
Here is the full report generated by ghostscript:
GPL Ghostscript 9.22 (2017-10-04)
Copyright (C) 2017 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefined in --get--
Operand stack:
   --nostringval--   --dict:9/18(ro)(L)--   112   --dict:13/13(L)--   --dict:13/13(L)--   base
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   2015   1   3   %oparray_pop   2014   1   3   %oparray_pop   1998   1   3   %oparray_pop   1884   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   %finish_stringwidth   --nostringval--   --nostringval--   14   8   0   --nostringval--   (pdf_text_enum_t)   %op_show_continue   --nostringval--
Dictionary stack:
   --dict:986/1684(ro)(G)--   --dict:1/20(G)--   --dict:82/200(L)--   --dict:5/6(ro)(L)--   --dict:180/300(L)--   --dict:44/200(L)--   --dict:8/17(L)--   --dict:51/90(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 58919
GPL Ghostscript 9.22: Unrecoverable error, exit code 1

Output - SVG, PDF

Traceback (most recent call last):
  File "Untitled.py", line 8, in <module>
    plt.savefig('fig.svg')
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 701, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1834, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2267, in print_figure
    **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 1193, in print_svg
    return self._print_svg(filename, svgwriter, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 1248, in _print_svg
    self.figure.draw(renderer)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1299, in draw
    renderer, self, artists, self.suppressComposite)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2437, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/axis.py", line 1147, in draw
    self.label.draw(renderer)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/text.py", line 762, in draw
    mtext=mtext)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 1150, in draw_tex
    self._draw_text_as_path(gc, x, y, s, prop, angle, ismath="TeX")
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 950, in _draw_text_as_path
    return_new_glyphs_only=True)
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/textpath.py", line 335, in get_glyphs_tex
    font_bunch = self.tex_font_map[dvifont.texname]
  File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/dviread.py", line 850, in __getitem__
    result = self._font[texname]
KeyError: b'tcss3583'

For PDF it's nearly identical; the last line reads:

KeyError: b'tcss1000'

In case of PS and SVG, no file is produced. In case of PDF, a corrupted file results. Saving to raster formats and inline previews in jupyter notebook work fine.

Matplotlib version

  • Operating system: Debian 10
  • Matplotlib version: 2.1.1
  • Python version: 3.6.4

Matplotlib from pip. Both Matplotlib and LaTeX are installed in userspace

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions