【发布时间】:2017-02-21 03:16:33
【问题描述】:
我正在尝试在 ipython 笔记本的 matplotlib 图中绘制 unicode 字符。
# For example:
plt.title("α")
但是,它给出了UnicodeEncodeError。
我已经尝试按照 matplotlib 文档中的建议导入 unicode_literals 模块:http://matplotlib.org/examples/text_labels_and_annotations/unicode_demo.html。
我也尝试过使用带有plt.title(u"α") 的unicode 字符串,如下所示:Unicode in ipython notebook。
这两种方法都没有影响错误输出,(我已将其放在底部)。
如何获取 unicode 字符串以在 ipython 笔记本的 matplotlib 中绘图?
如果可以的话,我对在这种情况下使用 LaTeX 不感兴趣。
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
305 pass
306 else:
--> 307 return printer(obj)
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
/usr/local/lib/python3.5/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
225
226 if 'png' in formats:
--> 227 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
228 if 'retina' in formats or 'png2x' in formats:
229 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
/usr/local/lib/python3.5/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
117
118 bytes_io = BytesIO()
--> 119 fig.canvas.print_figure(bytes_io, **kw)
120 data = bytes_io.getvalue()
121 if fmt == 'svg':
/usr/local/lib/python3.5/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2178 orientation=orientation,
2179 dryrun=True,
-> 2180 **kwargs)
2181 renderer = self.figure._cachedRenderer
2182 bbox_inches = self.figure.get_tightbbox(renderer)
/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
525
526 def print_png(self, filename_or_obj, *args, **kwargs):
--> 527 FigureCanvasAgg.draw(self)
528 renderer = self.get_renderer()
529 original_dpi = renderer.dpi
/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in draw(self)
472
473 try:
--> 474 self.figure.draw(self.renderer)
475 finally:
476 RendererAgg.lock.release()
/usr/local/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
60 def draw_wrapper(artist, renderer, *args, **kwargs):
61 before(artist, renderer)
---> 62 draw(artist, renderer, *args, **kwargs)
63 after(artist, renderer)
64
/usr/local/lib/python3.5/site-packages/matplotlib/figure.py in draw(self, renderer)
1157 dsu.sort(key=itemgetter(0))
1158 for zorder, a, func, args in dsu:
-> 1159 func(*args)
1160
1161 renderer.close_group('figure')
/usr/local/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
60 def draw_wrapper(artist, renderer, *args, **kwargs):
61 before(artist, renderer)
---> 62 draw(artist, renderer, *args, **kwargs)
63 after(artist, renderer)
64
/usr/local/lib/python3.5/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
2317
2318 for zorder, a in dsu:
-> 2319 a.draw(renderer)
2320
2321 renderer.close_group('axes')
/usr/local/lib/python3.5/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
60 def draw_wrapper(artist, renderer, *args, **kwargs):
61 before(artist, renderer)
---> 62 draw(artist, renderer, *args, **kwargs)
63 after(artist, renderer)
64
/usr/local/lib/python3.5/site-packages/matplotlib/text.py in draw(self, renderer)
747
748 with _wrap_text(self) as textobj:
--> 749 bbox, info, descent = textobj._get_layout(renderer)
750 trans = textobj.get_transform()
751
/usr/local/lib/python3.5/site-packages/matplotlib/text.py in _get_layout(self, renderer)
359 w, h, d = renderer.get_text_width_height_descent(clean_line,
360 self._fontproperties,
--> 361 ismath=ismath)
362 else:
363 w, h, d = 0, 0, 0
/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
227 fontsize = prop.get_size_in_points()
228 w, h, d = texmanager.get_text_width_height_descent(s, fontsize,
--> 229 renderer=self)
230 return w, h, d
231
/usr/local/lib/python3.5/site-packages/matplotlib/texmanager.py in get_text_width_height_descent(self, tex, fontsize, renderer)
673 else:
674 # use dviread. It sometimes returns a wrong descent.
--> 675 dvifile = self.make_dvi(tex, fontsize)
676 dvi = dviread.Dvi(dvifile, 72 * dpi_fraction)
677 try:
/usr/local/lib/python3.5/site-packages/matplotlib/texmanager.py in make_dvi(self, tex, fontsize)
397
398 if DEBUG or not os.path.exists(dvifile):
--> 399 texfile = self.make_tex(tex, fontsize)
400 outfile = basefile + '.output'
401 command = self._get_shell_cmd(
/usr/local/lib/python3.5/site-packages/matplotlib/texmanager.py in make_tex(self, tex, fontsize)
312 else:
313 try:
--> 314 fh.write(s.encode('ascii'))
315 except UnicodeEncodeError as err:
316 mpl.verbose.report("You are using unicode and latex, but "
UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in position 289: ordinal not in range(128)
【问题讨论】:
-
对我来说,
plt.title("α")在使用unicode_literals时效果很好。省略未来的导入,plt.title(u"α")也可以。所以我想你需要提供这个问题的minimal reproducible example。特别是在texmanager.py中,回溯似乎伴随着一些乳胶渲染,这表明您正在实际使用乳胶。这当然可能是与 unicode 字符一起出现的问题。
标签: python matplotlib plot unicode ipython