Unicode en ipython notebook

Estoy tratando de obtener un signo de “grado” (°) en un gráfico de matplotlib del cuaderno de ipython.

Cuando corro

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

Obtengo una ttwig, pero en lugar del signo de grado, tengo un cuadrado negro extraño con un signo de interrogación. Esto también ocurre cuando bash savefig la figura en PDF.

Si trato de correr

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

Me sale un error (ver abajo).

¿Alguna idea de lo que estoy haciendo mal?

 --------------------------------------------------------------------------- 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 

El mensaje de error le dice qué hacer:

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

Haz de tu xlabel una cadena Unicode:

 ax.set_xlabel(u"something at 55°") 

El problema es que la fuente que está utilizando para escribir la etiqueta no tiene el signo °. Intente utilizar el texto matemático de matplotlib en su lugar y formatee ° con látex:

 ax.set_xlabel("something at 55$^\circ$")