我正在尝试使用mathtext正确渲染我的图形轴标签(理想情况下我不想使用Latex本身来渲染文本)。但是,这个小的最小工作示例会产生如下所示的错误。在我看来它应该工作,可能是一个没有安装的必要包?
我正在运行matplotlib 2.2.2和Python 3.5
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(0,0)
ax.set_ylabel(r'$\mathrm A$')
产生的错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2516, in parse
result = self._expression.parseString(s)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1632, in parseString
raise exc
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1622, in parseString
loc, tokens = self._parse( instring, 0 )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3717, in parseImpl
return self.expr._parse( instring, loc, doActions, callPreParse=False )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3395, in parseImpl
loc, exprtokens = e._parse( instring, loc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3919, in parseImpl
return super(ZeroOrMore, self).parseImpl(instring, loc, doActions)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3848, in parseImpl
loc, tokens = self_expr_parse( instring, loc, doActions, callPreParse=False )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3378, in parseImpl
loc, resultlist = self.exprs[0]._parse( instring, loc, doActions, callPreParse=False )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1405, in _parseNoCache
tokens = fn( instring, tokensStart, retTokens )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1049, in wrapper
ret = func(*args[limit[0]:])
File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2585, in math_string
return self._math_expression.parseString(toks[0][1:-1])
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1632, in parseString
raise exc
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1622, in parseString
loc, tokens = self._parse( instring, 0 )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3717, in parseImpl
return self.expr._parse( instring, loc, doActions, callPreParse=False )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3848, in parseImpl
loc, tokens = self_expr_parse( instring, loc, doActions, callPreParse=False )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3717, in parseImpl
return self.expr._parse( instring, loc, doActions, callPreParse=False )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3530, in parseImpl
ret = e._parse( instring, loc, doActions )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
value = self._parseNoCache(instring, loc, doActions, callPreParse)
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1405, in _parseNoCache
tokens = fn( instring, tokensStart, retTokens )
File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1049, in wrapper
ret = func(*args[limit[0]:])
File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2687, in unknown_symbol
raise ParseFatalException(s, loc, "Unknown symbol: %s" % c)
pyparsing.ParseFatalException: Unknown symbol: \mathrm (at char 0), (line:1, col:1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 39, in on_draw_event
self._render_figure(w, h)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 30, in _render_figure
backend_agg.FigureCanvasAgg.draw(self)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 433, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axes/_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axis.py", line 1204, in draw
self.label.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/text.py", line 706, in draw
bbox, info, descent = textobj._get_layout(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/text.py", line 309, in _get_layout
ismath=ismath)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 237, in get_text_width_height_descent
self.mathtext_parser.parse(s, self.dpi, prop)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 3294, in parse
box = self._parser.parse(s, font_output, fontsize, dpi)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2522, in parse
six.text_type(err)]))
ValueError:
\mathrm A
^
Unknown symbol: \mathrm (at char 0), (line:1, col:1)
If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org
You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.
Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True
答案 0 :(得分:1)
你是正确的,在LaTeX中,\mathrm A
是有效的,但不要忘记Matplotlib解析器与LaTeX不同。
我在Python 3.6.4和Matplotlib 2.2.2上尝试了这个,错误不再存在。