Skip to content

usetex problem in Matplotlib 2.0 #8259

Closed

Description

@juliotux

Recently, after the matplotlib 1->2 update, some plots that worked in 1.5.3 stoped working in this new version. All of them use tex rendering for texts and math. Latex is correctly installed and the package is installed inside Anaconda 4.2.0.

If I set only

import matplotlib.pyplot as plt
import matplotlib
plt.rc('text', usetex=True)
matplotlib.rcParams.update({'font.size': 14})

The following errors are raised:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/dviread.py in __getitem__(self, texname)
    698         try:
--> 699             result = self._font[texname]
    700         except KeyError:

KeyError: 'cmr10'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
/home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_pdf.py in print_pdf(self, filename, **kwargs)
   2524                 bbox_inches_restore=_bbox_inches_restore)
-> 2525             self.figure.draw(renderer)
   2526             renderer.finalize()

...

/home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/dviread.py in __getitem__(self, texname)
    700         except KeyError:
--> 701             result = self._font[texname.decode('ascii')]
    702         fn, enc = result.filename, result.encoding

AttributeError: 'str' object has no attribute 'decode'

During handling of the above exception, another exception occurred:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-778cf2a0d47b> in <module>()
     37 plot_twiny(ax[0], 1850, 2020, 3)
     38 
---> 39 fig.savefig('./fig/ligth_curves.pdf')
     40 
     41 plt.show()

...

/home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/cbook.py in __call__(self, path)
   1028                 stat_key = realpath
   1029             else:
-> 1030                 stat = os.stat(realpath)
   1031                 stat_key = (stat.st_ino, stat.st_dev)
   1032             result = realpath, stat_key

FileNotFoundError: [Errno 2] No such file or directory: '/home/julio/Documents/docs/Artigos/Artigo 01 - R40 e R110/cmr10'

But, if I change the fonts to palatino, this plot is processed OK, but another plot in the same document raises similar error, but with "cmsy10" fonts.

Activity

  1. tacaswell commented on Mar 10, 2017

    @tacaswell
    Member

    Can you give an example of what you are doing that triggers this?

    Possibly related to #8039 and #6516

  2. tacaswell commented on Mar 10, 2017

    @tacaswell
    Member

    I can not reproduce this with a clean installation

    
    
    11:33 $ conda create -p /tmp/mpl_stock ipython matplotlib python
    Fetching package metadata .......
    Solving package specifications: ..........
    
    Package plan for installation in environment /tmp/mpl_stock:
    
    The following packages will be downloaded:
    
        package                    |            build
        ---------------------------|-----------------
        jpeg-9b                    |                0         941 KB  defaults
        path.py-10.1               |           py36_0          48 KB  defaults
        pygments-2.2.0             |           py36_0         1.3 MB  defaults
        traitlets-4.3.2            |           py36_0         130 KB  defaults
        ipython-5.3.0              |           py36_0         1.0 MB  defaults
        ------------------------------------------------------------
                                               Total:         3.4 MB
    
    The following NEW packages will be INSTALLED:
    
        cycler:           0.10.0-py36_0     defaults (soft-link)
        dbus:             1.10.10-0         defaults (soft-link)
        decorator:        4.0.11-py36_0     defaults (soft-link)
        expat:            2.1.0-0           defaults (soft-link)
        fontconfig:       2.12.1-3          defaults (soft-link)
        freetype:         2.5.5-2           defaults (soft-link)
        glib:             2.50.2-1          defaults (soft-link)
        gst-plugins-base: 1.8.0-0           defaults (soft-link)
        gstreamer:        1.8.0-0           defaults (soft-link)
        icu:              54.1-0            defaults (soft-link)
        ipython:          5.3.0-py36_0      defaults (soft-link)
        ipython_genutils: 0.1.0-py36_0      defaults (soft-link)
        jpeg:             9b-0              defaults (soft-link)
        libffi:           3.2.1-1           defaults (soft-link)
        libgcc:           5.2.0-0           defaults (soft-link)
        libiconv:         1.14-0            defaults (soft-link)
        libpng:           1.6.27-0          defaults (soft-link)
        libxcb:           1.12-1            defaults (soft-link)
        libxml2:          2.9.4-0           defaults (soft-link)
        matplotlib:       2.0.0-np112py36_0 defaults (soft-link)
        mkl:              2017.0.1-0        defaults (soft-link)
        numpy:            1.12.0-py36_0     defaults (soft-link)
        openssl:          1.0.2k-1          defaults (soft-link)
        path.py:          10.1-py36_0       defaults (soft-link)
        pcre:             8.39-1            defaults (soft-link)
        pexpect:          4.2.1-py36_0      defaults (soft-link)
        pickleshare:      0.7.4-py36_0      defaults (soft-link)
        pip:              9.0.1-py36_1      defaults (soft-link)
        prompt_toolkit:   1.0.9-py36_0      defaults (soft-link)
        ptyprocess:       0.5.1-py36_0      defaults (soft-link)
        pygments:         2.2.0-py36_0      defaults (soft-link)
        pyparsing:        2.1.4-py36_0      defaults (soft-link)
        pyqt:             5.6.0-py36_2      defaults (soft-link)
        python:           3.6.0-0           defaults (soft-link)
        python-dateutil:  2.6.0-py36_0      defaults (soft-link)
        pytz:             2016.10-py36_0    defaults (soft-link)
        qt:               5.6.2-3           defaults (soft-link)
        readline:         6.2-2             defaults (soft-link)
        setuptools:       27.2.0-py36_0     defaults (soft-link)
        simplegeneric:    0.8.1-py36_1      defaults (soft-link)
        sip:              4.18-py36_0       defaults (soft-link)
        six:              1.10.0-py36_0     defaults (soft-link)
        sqlite:           3.13.0-0          defaults (soft-link)
        tk:               8.5.18-0          defaults (soft-link)
        traitlets:        4.3.2-py36_0      defaults (soft-link)
        wcwidth:          0.1.7-py36_0      defaults (soft-link)
        wheel:            0.29.0-py36_0     defaults (soft-link)
        xz:               5.2.2-1           defaults (soft-link)
        zlib:             1.2.8-3           defaults (soft-link)
    
    Proceed ([y]/n)? 
    
    Pruning fetched packages from the cache ...
    Fetching packages ...
    jpeg-9b-0.tar. 100% |########################################################################################################################################################################################################################################################################################################################################################################################| Time: 0:00:00  15.30 MB/s
    path.py-10.1-p 100% |########################################################################################################################################################################################################################################################################################################################################################################################| Time: 0:00:00   2.41 MB/s
    pygments-2.2.0 100% |########################################################################################################################################################################################################################################################################################################################################################################################| Time: 0:00:00  19.03 MB/s
    traitlets-4.3. 100% |########################################################################################################################################################################################################################################################################################################################################################################################| Time: 0:00:00   3.94 MB/s
    ipython-5.3.0- 100% |########################################################################################################################################################################################################################################################################################################################################################################################| Time: 0:00:00  14.29 MB/s
    Extracting packages ...
    [      COMPLETE      ]|###########################################################################################################################################################################################################################################################################################################################################################################################################| 100%
    Linking packages ...
    [      COMPLETE      ]|###########################################################################################################################################################################################################################################################################################################################################################################################################| 100%
    #
    # To activate this environment, use:
    # > source activate /tmp/mpl_stock
    #
    # To deactivate this environment, use:
    # > source deactivate /tmp/mpl_stock
    #
    
    ✔ ~/
    11:34 $ source activate /tmp/mpl_stock
    (mpl_stock) ✔ ~/
    11:34 $ ipython
    Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 5.3.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]: import matplotlib.pyplot as plt
       ...: import matplotlib
       ...: plt.rc('text', usetex=True)
       ...: matplotlib.rcParams.update({'font.size': 14})
       ...: plt.plot(range(5))
       ...: plt.savefig('test.pdf')
       ...:  
    
    In [2]: !okular test.pdf
    
    
    In [3]: 
    
    
  3. added this to the milestone on Mar 10, 2017
  4. juliotux commented on Mar 10, 2017

    @juliotux
    Author

    Doing a simple plot inside Jupyter notebook.

    import matplotlib
    matplotlib.rcParams.update({'font.size': 14})
    %matplotlib notebook
    import matplotlib.pyplot as plt
    plt.rc('text', usetex=True)
    
    fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    ax.plot([0,1,2,3,4], [4,3,2,1,0])
    fig.savefig('dummy.pdf')
    plt.show()
    

    The same error is returned:

    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/dviread.py in __getitem__(self, texname)
        698         try:
    --> 699             result = self._font[texname]
        700         except KeyError:
    
    KeyError: 'cmr12'
    
    During handling of the above exception, another exception occurred:
    
    AttributeError                            Traceback (most recent call last)
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_pdf.py in print_pdf(self, filename, **kwargs)
       2524                 bbox_inches_restore=_bbox_inches_restore)
    -> 2525             self.figure.draw(renderer)
       2526             renderer.finalize()
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
         62         before(artist, renderer)
    ---> 63         draw(artist, renderer, *args, **kwargs)
         64         after(artist, renderer)
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/figure.py in draw(self, renderer)
       1142             mimage._draw_list_compositing_images(
    -> 1143                 renderer, self, dsu, self.suppressComposite)
       1144 
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
        138         for zorder, a in dsu:
    --> 139             a.draw(renderer)
        140     else:
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
         62         before(artist, renderer)
    ---> 63         draw(artist, renderer, *args, **kwargs)
         64         after(artist, renderer)
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
       2408 
    -> 2409         mimage._draw_list_compositing_images(renderer, self, dsu)
       2410 
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
        138         for zorder, a in dsu:
    --> 139             a.draw(renderer)
        140     else:
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
         62         before(artist, renderer)
    ---> 63         draw(artist, renderer, *args, **kwargs)
         64         after(artist, renderer)
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/axis.py in draw(self, renderer, *args, **kwargs)
       1140         for tick in ticks_to_draw:
    -> 1141             tick.draw(renderer)
       1142 
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
         62         before(artist, renderer)
    ---> 63         draw(artist, renderer, *args, **kwargs)
         64         after(artist, renderer)
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/axis.py in draw(self, renderer)
        263         if self.label1On:
    --> 264             self.label1.draw(renderer)
        265         if self.label2On:
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
         62         before(artist, renderer)
    ---> 63         draw(artist, renderer, *args, **kwargs)
         64         after(artist, renderer)
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/text.py in draw(self, renderer)
        797                                           textobj._fontproperties, angle,
    --> 798                                           mtext=mtext)
        799                 else:
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_pdf.py in draw_tex(self, gc, x, y, s, prop, angle, ismath, mtext)
       1862                 if dvifont.texname not in self.file.dviFontInfo:
    -> 1863                     psfont = self.tex_font_mapping(dvifont.texname)
       1864                     self.file.dviFontInfo[dvifont.texname] = Bunch(
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_pdf.py in tex_font_mapping(self, texfont)
       1565                 dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))
    -> 1566         return self.tex_font_map[texfont]
       1567 
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/dviread.py in __getitem__(self, texname)
        700         except KeyError:
    --> 701             result = self._font[texname.decode('ascii')]
        702         fn, enc = result.filename, result.encoding
    
    AttributeError: 'str' object has no attribute 'decode'
    
    During handling of the above exception, another exception occurred:
    
    FileNotFoundError                         Traceback (most recent call last)
    <ipython-input-1-4a825c653489> in <module>()
          7 fig, ax = plt.subplots(1, 1, figsize=(8, 8))
          8 ax.plot([0,1,2,3,4], [4,3,2,1,0])
    ----> 9 fig.savefig('dummy.pdf')
         10 plt.show()
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/figure.py in savefig(self, *args, **kwargs)
       1570             self.set_frameon(frameon)
       1571 
    -> 1572         self.canvas.print_figure(*args, **kwargs)
       1573 
       1574         if frameon:
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
       2242                 orientation=orientation,
       2243                 bbox_inches_restore=_bbox_inches_restore,
    -> 2244                 **kwargs)
       2245         finally:
       2246             if bbox_inches and restore_bbox:
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_pdf.py in print_pdf(self, filename, **kwargs)
       2529                 file.endStream()
       2530             else:            # we opened the file above; now finish it off
    -> 2531                 file.close()
       2532 
       2533 
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_pdf.py in close(self)
        545         self.endStream()
        546         # Write out the various deferred objects
    --> 547         self.writeFonts()
        548         self.writeObject(self.alphaStateObject,
        549                          dict([(val[0], val[1])
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_pdf.py in writeFonts(self)
        645                 # a normal TrueType font
        646                 matplotlib.verbose.report('Writing TrueType font', 'debug')
    --> 647                 realpath, stat_key = get_realpath_and_stat(filename)
        648                 chars = self.used_characters.get(stat_key)
        649                 if chars is not None and len(chars[1]):
    
    /home/julio/.anaconda3/lib/python3.5/site-packages/matplotlib/cbook.py in __call__(self, path)
       1028                 stat_key = realpath
       1029             else:
    -> 1030                 stat = os.stat(realpath)
       1031                 stat_key = (stat.st_ino, stat.st_dev)
       1032             result = realpath, stat_key
    
    FileNotFoundError: [Errno 2] No such file or directory: '/home/julio/Documents/cmr12'
    

    Running the same example inside an ipython terminal, the same occurs.

  5. juliotux commented on Mar 10, 2017

    @juliotux
    Author

    No problem if save as png or jpeg.

  6. tacaswell commented on Mar 10, 2017

    @tacaswell
    Member

    FileNotFoundError: [Errno 2] No such file or directory: '/home/julio/Documents/cmr12' that seems wrong, not sure why it is looking there for the fonts.

    Can you try removing all of the files in ~/.cache/matplotlib ?

  7. juliotux commented on Mar 10, 2017

    @juliotux
    Author

    I removed the matplotlib cache before and tried again, without success. As aditional information, this problem only occurs when save plots as pdf format.

  8. juliotux commented on Mar 10, 2017

    @juliotux
    Author

    Problem fixed generating a local map of fonts using updmap command. Apparently, Arch Linux don't do it when installing texlive.

  9. juliotux commented on Mar 10, 2017

    @juliotux
    Author

    Installed from default Arch package with pacman. The main problem I think is that the Matplotlib requires a local map of fonts, which is not created by the package, that creates only a system version of it.

    Now I think the question is: why do Matplolib 1.5.3 runs normally without this local map and 2.0 doesn't?

  10. modified the milestones: , on May 3, 2017
  11. 13 remaining items

  12. jkseppan commented on Aug 3, 2017

    @jkseppan
    Member

    Reproduction instructions: I used Linode's "Arch 2017.07.1" and ran

    pacman -Syu
    pacman -S ipython python-matplotlib texlive-latexextra texlive-fontsextra
    

    and accepted all defaults. Then I started ipython and ran the following:

    import matplotlib
    matplotlib.use('pdf')
    matplotlib.rcParams.update({'font.size': 14})
    import matplotlib.pyplot as plt
    plt.rc('text', usetex=True)
    fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    ax.plot([0,1,2,3,4], [4,3,2,1,0])
    fig.savefig('dummy.pdf')

    I got a similar error as reported above.

  13. jkseppan commented on Aug 3, 2017

    @jkseppan
    Member

    The command updmap --cnffile /etc/texmf/web2c/updmap.cfg -sys fixes the map files so that the cmr12 font is found, and the script works. Before that command none of the map files I could find included that font.

  14. ClaymorePT commented on Aug 3, 2017

    @ClaymorePT
  15. andrmnl commented on Aug 3, 2017

    @andrmnl
  16. jkseppan commented on Aug 3, 2017

    @jkseppan
    Member

    I think I agree with the comment saying that this is an Arch Linux configuration issue rather than a Matplotlib bug. The error message could be clearer, though.

    Possibly related: the following commit deletes the upstream updmap configuration, commenting that it "contains too many maps".

    https://git.archlinux.org/svntogit/packages.git/commit/trunk/PKGBUILD?h=packages/texlive-core&id=7eaea8d4f608f13bb5afc4394ed6f4624ded1fd4

  17. andrmnl commented on Aug 3, 2017

    @andrmnl
  18. felixonmars commented on Aug 3, 2017

    @felixonmars
    Contributor

    Adds some info: I was getting a (maybe) similar problem with pypandoc's test suite: JessicaTegner/pypandoc#132

  19. jkseppan commented on Aug 4, 2017

    @jkseppan
    Member
  20. modified the milestones: , on Oct 9, 2017
  21. lisette-espin commented on Apr 1, 2019

    @lisette-espin

    You just need to make sure all your text follows LaTex conventions.
    Eg. "% coverage" --> r"$%coverage$"

  22. anntzer commented on Jan 24, 2021

    @anntzer
    Contributor

    FWIW I cannot reproduce this anymore using a clean Arch Linux docker image and @jkseppan's instructions. I'll optimistically assume that this has been fixed in the meantime either on Arch's side or on Matplotlib's side, but feel free to reopen with a more recent repro.

  23. removed this from the future releases milestone on Oct 6, 2022
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions