【问题标题】:Unicode fails in matplotlib legend label, with "Package inputenc Error: Unicode character ... (inputenc) ... not set up for use with LaTeX."Unicode 在 matplotlib 图例标签中失败,显示“Package inputenc Error: Unicode character ... (inputenc) ... not set up for use with LaTeX.”
【发布时间】:2021-08-31 13:17:05
【问题描述】:

我在 matplotlib 中的图例标签因 unicode 字符而失败。例如,

#!/usr/bin/python
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

plt.plot(0,0, label='foo Д')
plt.legend()

失败:

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.

如何在我的图例标签中轻松包含多种不同的外语元素?

更新: (@mlabuda2) 添加

plt.rcParams.update({
    "pgf.texsystem": "xelatex",
    "pgf.preamble": "\n".join([
         r"\usepackage[utf8x]{inputenc}",
         r"\usepackage[T1]{fontenc}",
         r"\usepackage{cmbright}",
    ]),
})

没有区别。

啊,但以下内容适用于包含的字符(示例问题由此解决):

#!/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')

但是,对于中文字符,这仍然失败。

【问题讨论】:

    标签: python matplotlib unicode latex


    【解决方案1】:

    尝试使用另一个后端,例如。 Xealtex:https://matplotlib.org/stable/tutorials/text/pgf.html。 根据这个LINK,它应该适用于各种外语。

    【讨论】:

    • 谢谢。该页面说 xelatex 是默认设置。我在尝试中添加了评论。
    • 但我找到了解决方案。这是一个不同的 matplotlib 后端,而不是不同的 LaTeX 后端。它只是添加一行 (use('pgf')) 。如果你愿意,你可以编辑你的答案,或者我会写一个。
    【解决方案2】:

    添加标题行

    import matplotlib
    matplotlib.use('pgf')
    

    解决了该角色以及许多其他角色的问题。但还不是我的汉字。那就是:

    这个后端没有交互式绘图并且非常慢。 但是您可以使用savefig 将绘图/输出呈现为 pgf 或 pdf。现在在问题描述中查看最后的更新/评论。

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      相关资源
      最近更新 更多