Skip to content

Broken pdf export when using genuine TeX (Missing encode) #9702

Closed

Description

@stippingerm

Bug report

Missing encode for pdf export with TeX

When exporting a pdf figure that uses the real TeX interpreter (not the one built into Matplotlib), either using PdfPages or savefig, the error message "TypeError: a bytes-like object is required, not 'str'" appears.

Code for reproduction

import matplotlib.pyplot as plt

plt.rc('text', usetex=True) # Without this line the error is not reproduced
fig = plt.figure(figsize=(3, 3))
plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
plt.title('Page One')
plt.savefig('testpage.pdf') # error message at this line
plt.close()

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-18ae62d6b3c1> in <module>()
      6 plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
      7 plt.title('Page One')
----> 8 plt.savefig('testpage.pdf')  # saves the current figure into a pdf page
      9 plt.close()

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py in savefig(*args, **kwargs)
    695 def savefig(*args, **kwargs):
    696     fig = gcf()
--> 697     res = fig.savefig(*args, **kwargs)
    698     fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
    699     return res

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py in savefig(self, fname, **kwargs)
   1812             self.set_frameon(frameon)
   1813 
-> 1814         self.canvas.print_figure(fname, **kwargs)
   1815 
   1816         if frameon:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2257                 orientation=orientation,
   2258                 bbox_inches_restore=_bbox_inches_restore,
-> 2259                 **kwargs)
   2260         finally:
   2261             if bbox_inches and restore_bbox:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py in print_pdf(self, filename, **kwargs)
   2590                 RendererPdf(file, image_dpi, height, width),
   2591                 bbox_inches_restore=_bbox_inches_restore)
-> 2592             self.figure.draw(renderer)
   2593             renderer.finalize()
   2594             file.finalize()

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
   1293 
   1294             mimage._draw_list_compositing_images(
-> 1295                 renderer, self, artists, self.suppressComposite)
   1296 
   1297             renderer.close_group('figure')

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    136     if not_composite or not has_images:
    137         for a in artists:
--> 138             a.draw(renderer)
    139     else:
    140         # Composite any adjacent images together

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2397             renderer.stop_rasterizing()
   2398 
-> 2399         mimage._draw_list_compositing_images(renderer, self, artists)
   2400 
   2401         renderer.close_group('axes')

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    136     if not_composite or not has_images:
    137         for a in artists:
--> 138             a.draw(renderer)
    139     else:
    140         # Composite any adjacent images together

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in draw(self, renderer, *args, **kwargs)
   1136 
   1137         for tick in ticks_to_draw:
-> 1138             tick.draw(renderer)
   1139 
   1140         # scale up the axis label box to also find the neighbors, not

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in draw(self, renderer)
    280 
    281         if self.label1On:
--> 282             self.label1.draw(renderer)
    283         if self.label2On:
    284             self.label2.draw(renderer)

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\text.py in draw(self, renderer)
    797                     textrenderer.draw_tex(gc, x, y, clean_line,
    798                                           textobj._fontproperties, angle,
--> 799                                           mtext=mtext)
    800                 else:
    801                     textrenderer.draw_text(gc, x, y, clean_line,

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py in draw_tex(self, gc, x, y, s, prop, angle, ismath, mtext)
   1942         for x1, y1, dvifont, glyph, width in page.text:
   1943             if dvifont != oldfont:
-> 1944                 pdfname = self.file.dviFontName(dvifont)
   1945                 seq += [['font', pdfname, dvifont.size]]
   1946                 oldfont = dvifont

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py in dviFontName(self, dvifont)
    686             return dvi_info.pdfname
    687 
--> 688         psfont = self.texFontMap[dvifont.texname]
    689         if psfont.filename is None:
    690             raise ValueError(

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in __getitem__(self, texname)
    864         fn, enc = result.filename, result.encoding
    865         if fn is not None and not fn.startswith(b'/'):
--> 866             fn = find_tex_file(fn)
    867         if enc is not None and not enc.startswith(b'/'):
    868             enc = find_tex_file(result.encoding)

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in find_tex_file(filename, format)
   1043     # https://github.com/matplotlib/matplotlib/issues/633
   1044     pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-> 1045                             stderr=subprocess.PIPE)
   1046     result = pipe.communicate()[0].rstrip()
   1047     matplotlib.verbose.report('find_tex_file result: %s' % result,

C:\ProgramData\Anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    707                                 c2pread, c2pwrite,
    708                                 errread, errwrite,
--> 709                                 restore_signals, start_new_session)
    710         except:
    711             # Cleanup if the child failed starting.

C:\ProgramData\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
    969 
    970             if not isinstance(args, str):
--> 971                 args = list2cmdline(args)
    972 
    973             # Process startup details

C:\ProgramData\Anaconda3\lib\subprocess.py in list2cmdline(seq)
    459             result.append(' ')
    460 
--> 461         needquote = (" " in arg) or ("\t" in arg) or not arg
    462         if needquote:
    463             result.append('"')

TypeError: a bytes-like object is required, not 'str'

Expected outcome

Normally the real Tex interpreter must work as well, not just the builtin one. The line with savefig should produce a pdf file not an error message. This issue appeared after updating to python 3.6 which differentiates bytes-like and unicode string object.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.1.0 py36h11b4b9c_0
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: Anaconda python 3.6.3 h9e2ca53_1
  • Jupyter version (if applicable: IPython 6.1.0
  • Other libraries: n.a.

Activity

  1. changed the title [-]Missing encode for pdf export with genuine TeX[/-] [+]Broken pdf export when using genuine TeX (Missing encode)[/+] on Nov 6, 2017
  2. jklymak commented on Nov 6, 2017

    @jklymak
    Member

    I'm afraid I don't get this on Master on macOS from the command line or ipython... Maybe this has been fixed though a quick search doesn't make that obvious to me.

  3. afvincent commented on Nov 6, 2017

    @afvincent
    Contributor

    @jklymak Isn't it somehow related to #9292?

  4. jklymak commented on Nov 6, 2017

    @jklymak
    Member

    Ah yeah, that looks right. @stippingerm this looks to have been fixed, but feel free to re-open if you think it hasn't. 2.1.1 should be out "soonish"...

  5. DrunkenGMaster commented on Nov 20, 2017

    @DrunkenGMaster

    I updated to Matplotlib 2.1.0-py36h11b4b9c_0 today. The bug was still present. I assume it will be fixed in 2.1.1

  6. added this to the v2.1.1 milestone on Nov 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions