7

我试图从 ipython 笔记本的 matplotlib 图中获得一个“度”符号(°)。

当我跑

ax = plt.gca()
ax.set_xlabel("something at 55" + unicode("\xc2", errors='replace'))
ax.plot([0.,1.,], [0.,1.])

我得到了一个情节,但不是度数符号,而是一个带有问号的奇怪黑色方块。当我尝试savefig将图形转换为 PDF 时也会发生这种情况。

如果我尝试跑步

ax = plt.gca()
ax.set_xlabel("something at 55°")
ax.plot([0.,1.,], [0.,1.])

我收到一个错误(见下文)。

知道我做错了什么吗?

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/zmq/pylab/backend_inline.pyc in show(close)
    100     try:
    101         for figure_manager in Gcf.get_all_fig_managers():
--> 102             send_figure(figure_manager.canvas.figure)
    103     finally:
    104         show._to_draw = []

/usr/lib/python2.7/dist-packages/IPython/zmq/pylab/backend_inline.pyc in send_figure(fig)
    188     """
    189     fmt = InlineBackend.instance().figure_format
--> 190     data = print_figure(fig, fmt)
    191     # print_figure will return None if there's nothing to draw:

    192     if data is None:

/usr/lib/python2.7/dist-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt)
    102     try:
    103         bytes_io = BytesIO()
--> 104         fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight')
    105         data = bytes_io.getvalue()
    106     finally:

/usr/lib/pymodules/python2.7/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   1981                     orientation=orientation,
   1982                     dryrun=True,
-> 1983                     **kwargs)
   1984                 renderer = self.figure._cachedRenderer
   1985                 bbox_inches = self.figure.get_tightbbox(renderer)

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    467 
    468     def print_png(self, filename_or_obj, *args, **kwargs):
--> 469         FigureCanvasAgg.draw(self)
    470         renderer = self.get_renderer()
    471         original_dpi = renderer.dpi

/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.pyc in draw(self)
    419 
    420         try:
--> 421             self.figure.draw(self.renderer)
    422         finally:
    423             RendererAgg.lock.release()

/usr/lib/pymodules/python2.7/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/lib/pymodules/python2.7/matplotlib/figure.pyc in draw(self, renderer)
    896         dsu.sort(key=itemgetter(0))
    897         for zorder, a, func, args in dsu:
--> 898             func(*args)
    899 
    900         renderer.close_group('figure')

/usr/lib/pymodules/python2.7/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in draw(self, renderer, inframe)
   1995 
   1996         for zorder, a in dsu:
-> 1997             a.draw(renderer)
   1998 
   1999         renderer.close_group('axes')

/usr/lib/pymodules/python2.7/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/lib/pymodules/python2.7/matplotlib/axis.pyc in draw(self, renderer, *args, **kwargs)
   1052         self._update_label_position(ticklabelBoxes, ticklabelBoxes2)
   1053 
-> 1054         self.label.draw(renderer)
   1055 
   1056         self._update_offset_text_position(ticklabelBoxes, ticklabelBoxes2)

/usr/lib/pymodules/python2.7/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/lib/pymodules/python2.7/matplotlib/text.pyc in draw(self, renderer)
    524         renderer.open_group('text', self.get_gid())
    525 
--> 526         bbox, info = self._get_layout(renderer)
    527         trans = self.get_transform()
    528 

/usr/lib/pymodules/python2.7/matplotlib/text.pyc in _get_layout(self, renderer)
    303         baseline = 0
    304         for i, line in enumerate(lines):
--> 305             clean_line, ismath = self.is_math_text(line)
    306             if clean_line:
    307                 w, h, d = get_text_width_height_descent(clean_line,

/usr/lib/pymodules/python2.7/matplotlib/text.pyc in is_math_text(s)
    987             return s, 'TeX'
    988 
--> 989         if cbook.is_math_text(s):
    990             return s, True
    991         else:

/usr/lib/pymodules/python2.7/matplotlib/cbook.pyc in is_math_text(s)
   1836     except UnicodeDecodeError:
   1837         raise ValueError(
-> 1838             "matplotlib display text must have all code points < 128 or use Unicode strings")
   1839 
   1840     dollar_count = s.count(r'$') - s.count(r'\$')

ValueError: matplotlib display text must have all code points < 128 or use Unicode strings
4

2 回答 2

16

错误消息告诉您该怎么做:

ValueError: matplotlib display text must have all code points < 128 
or use Unicode strings

制作xlabel一个 unicode 字符串:

ax.set_xlabel(u"something at 55°")
于 2012-11-05T16:27:54.037 回答
5

问题是您用于编写标签的字体没有° 符号。尝试改用 matplotlib 的mathtext并用乳胶格式化°:

ax.set_xlabel("something at 55$^\circ$")
于 2012-11-05T16:31:36.680 回答