I'm trying to get a "degree" sign (°) in a matplotlib plot from ipython notebook.
When I run
ax = plt.gca()
ax.set_xlabel("something at 55" + unicode("\xc2", errors='replace'))
ax.plot([0.,1.,], [0.,1.])
I get a plot, but instead of the degree sign, I have a strange black square with a question mark. This also happens when I try to savefig
the figure to PDF.
If I try to run
ax = plt.gca()
ax.set_xlabel("something at 55°")
ax.plot([0.,1.,], [0.,1.])
I get an error (see below).
Any idea what I'm doing wrong?
---------------------------------------------------------------------------
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
The error message is telling you what to do:
Make your
xlabel
a unicode string:The problem is that the font you are using to write the label does not have the ° sign. Try to use matplotlib's mathtext instead and format the ° with latex: