Skip to content

usetex problem in Matplotlib 2.0 #8259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
juliotux opened this issue Mar 10, 2017 · 29 comments
Closed

usetex problem in Matplotlib 2.0 #8259

juliotux opened this issue Mar 10, 2017 · 29 comments

Comments

@juliotux
Copy link

juliotux commented Mar 10, 2017

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.

@tacaswell
Copy link
Member

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

Possibly related to #8039 and #6516

@tacaswell
Copy link
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]: 

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Mar 10, 2017
@juliotux
Copy link
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.

@juliotux
Copy link
Author

No problem if save as png or jpeg.

@tacaswell
Copy link
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 ?

@juliotux
Copy link
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.

@juliotux
Copy link
Author

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

@anntzer
Copy link
Contributor

anntzer commented Mar 10, 2017

@juliotux
Copy link
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?

@ghost
Copy link

ghost commented Apr 3, 2017

I'm getting the same error even after generating a local font map with the updmap command. Also using Arch. Any ideas?

@QuLogic QuLogic modified the milestones: 2.0.1 (next bug fix release), 2.0.2 (next bug fix release) May 3, 2017
@ClaymorePT
Copy link

Having the same issue with non-Type 1 fonts...
I'm trying to build a PDF out of a plot using PT Mono font. Having the same issue :/

@amit112amit
Copy link

I am on latest version of Manjaro KDE, using Spyder 3.1.3 with Python 3.6. I get the same error about 'cmr10'. As @juliotux observed, updmap worked for me too.

@amanoel
Copy link

amanoel commented May 13, 2017

Hello,

Another Arch user here with the same problem. I've also solved it by running updmap, but
I lost some time figuring it out: for some reason, the main updmap.cfg file at /etc/texmf/web2c couldn't be found by updmap (if I run kpsewhich --all updmap.cfg there is no output). It wasn't obvious that was the problem, since no error message is produced.

Finally I was able to solve it by running updmap --cnffile /etc/texmf/web2c/updmap.cfg.

@jkseppan
Copy link
Member

The code runs kpsewhich pdftex.map to find the font mapping, which is a somewhat arbitrary choice but worked on all the systems I tested on. Does this not find a usable mapping on Arch Linux, and is there some other font mapping (or way of finding the font mapping) that works better?

@jkseppan
Copy link
Member

Also, can someone point me to a Vagrantfile or another way to install an Arch Linux distribution in a virtual machine, with the Matplotlib prerequisites and enough of texlive installed to see this bug?

@amanoel
Copy link

amanoel commented May 13, 2017 via email

@juliotux
Copy link
Author

Another interesting infos that can be related to this problem in Arch. Before install Matplotlib, I didn't have any problem to compile generic .tex files (with texlive-most already installed). But, for some reason, after the install, all the tex compiling stopped to work until run the updmap command.

I don't know too much about other distros, but Arch uses two font maps, one owned by system and generated by updmap-sys with root, and other owned by user generated by updmap. By default, before install Matplotlib the tex compiler uses the system font map, but after the install the system changes to the user font map, that initially is not fully generated (if the Matplotlib is installed locally, like in Anaconda).

@tacaswell
Copy link
Member

attn @felixonmars any insight from the arch side?

@felixonmars
Copy link
Contributor

Sorry but I am not familiar with TeX at all :(

@jkseppan
Copy link
Member

jkseppan commented Aug 3, 2017

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.

@jkseppan
Copy link
Member

jkseppan commented Aug 3, 2017

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.

@ClaymorePT
Copy link

ClaymorePT commented Aug 3, 2017 via email

@amanoel
Copy link

amanoel commented Aug 3, 2017 via email

@jkseppan
Copy link
Member

jkseppan commented Aug 3, 2017

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

@amanoel
Copy link

amanoel commented Aug 3, 2017 via email

@felixonmars
Copy link
Contributor

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

@jkseppan
Copy link
Member

jkseppan commented Aug 4, 2017 via email

@tacaswell tacaswell modified the milestones: 2.1.1 (next bug fix release), 2.2 (next feature release) Oct 9, 2017
@lisette-espin
Copy link

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

@anntzer
Copy link
Contributor

anntzer commented Jan 24, 2021

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.

@anntzer anntzer closed this as completed Jan 24, 2021
@story645 story645 removed this from the future releases milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests