My legend labels in matplotlib are failing with unicode characters. For instance,
#!/usr/bin/python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
plt.plot(0,0, label='foo Д')
plt.legend()
fails with:
Traceback (most recent call last):
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/texmanager.py", line 252, in _run_checked_subprocess
report = subprocess.check_output(
File "/usr/lib/python3.9/subprocess.py", line 424, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/lib/python3.9/subprocess.py", line 528, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '/home/meuser/.cache/matplotlib/tex.cache/4adc63a3e97a497d437ed3e0dc1562f2.tex']' returned
non-zero exit status 1.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/backends/backend_qt5.py", line 475, in _draw_idle
self.draw()
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 406, in draw
self.figure.draw(self.renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/figure.py", line 2780, in draw
mimage._draw_list_compositing_images(
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 431, in wrapper
return func(*inner_args, **inner_kwargs)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 2921, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/legend.py", line 606, in draw
bbox = self._legend_box.get_window_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 352, in get_window_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 438, in get_extent_offsets
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 438, in <listcomp>
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 474, in get_extent_offsets
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 474, in <listcomp>
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 438, in get_extent_offsets
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 438, in <listcomp>
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 474, in get_extent_offsets
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 474, in <listcomp>
whd_list = [c.get_extent(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/offsetbox.py", line 823, in get_extent
bbox, info, yd = self._text._get_layout(renderer)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/text.py", line 314, in _get_layout
w, h, d = renderer.get_text_width_height_descent(
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 229, in get_text_width_height_descent
w, h, d = texmanager.get_text_width_height_descent(
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/texmanager.py", line 399, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/texmanager.py", line 291, in make_dvi
self._run_checked_subprocess(
File "/home/meuser/.local/lib/python3.9/site-packages/matplotlib/texmanager.py", line 260, in _run_checked_subprocess
raise RuntimeError(
RuntimeError: latex was not able to process the following string:
b'\u0414\u0440\u0443\u0436\u0431\u0430'
Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/Debian) (preloaded format=latex)
restricted write18 enabled.
entering extended mode
(/home/meuser/.cache/matplotlib/tex.cache/4adc63a3e97a497d437ed3e0dc1562f2.tex
LaTeX2e <2020-10-01> patch level 4
L3 programming layer <2021-01-09> xparse <2020-03-03>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texmf/tex/latex/cm-super/type1ec.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty)
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/iftex/ifvtex.sty
(/usr/share/texlive/texmf-dist/tex/generic/iftex/iftex.sty)))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty))
(/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty
(/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty))
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty)
(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
No file 4adc63a3e97a497d437ed3e0dc1562f2.aux.
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Package inputenc Error: Unicode character Д (U+0414)
(inputenc) not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
l.19 {sffamily Д
ружба}
No pages of output.
Transcript written on 4adc63a3e97a497d437ed3e0dc1562f2.log.
How can I include multiple various foreign language elements in my legend labels without trouble?
Updates:
(@mlabuda2) Adding
plt.rcParams.update({
"pgf.texsystem": "xelatex",
"pgf.preamble": "n".join([
r"usepackage[utf8x]{inputenc}",
r"usepackage[T1]{fontenc}",
r"usepackage{cmbright}",
]),
})
Makes no difference.
Ah, but the following works fine for the character included (example problem thus solved):
#!/usr/bin/python
# -*- coding: utf-8 -*-
import matplotlib
matplotlib.use('pgf')
import matplotlib.pyplot as plt
plt.plot(0,0, label='foo Д')
plt.legend()
plt.savefig('bugshow.pdf')
However, this still fails for Chinese characters.
2
Answers
Adding the header line
solves the problem for that character, and many others. But not yet for my Chinese characters. That is:
This backend has no interactive plots and is very slow. But you can render the plots/ output as pgf or pdf with
savefig
. See the last update/comment now in the Question description.Try to use another backend eg. Xealtex: https://matplotlib.org/stable/tutorials/text/pgf.html.
According to this LINK it should work with various foreign languages.