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

xlabelUnicode文字列を作成します。

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 に答える