gpt4 book ai didi

python - ipython笔记本中的Unicode

转载 作者:太空狗 更新时间:2023-10-30 00:23:36 25 4
gpt4 key购买 nike

我正在尝试从 ipython notebook 的 matplotlib 图中获取“度”符号 (°)。

当我运行时

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

我得到了一个情节,但我没有度数符号,而是一个带问号的奇怪黑色方 block 。当我尝试将图形 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

最佳答案

错误信息告诉你该怎么做:

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

让你的 xlabel 成为一个 unicode 字符串:

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

关于python - ipython笔记本中的Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13236086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com