Python matplotlib UnicodeEncodeError在interactive mod中重置rcParams时出错

2024-09-24 22:26:56 发布

您现在位置:Python中文网/ 问答频道 /正文

Python在重置rcParams并在交互模式下使用Latex时显示UnicodeEncodeError。我使用PyCharm 2018.1.4和iPython 5.8。Python版本是2.7.15,Matplotlib版本是2.2.2。你知道吗

输入1创建绘图,但给出UnicodeEncodeError:

Python 2.7.15 |Anaconda custom (64-bit)| (default, May  1 2018, 23:32:55) 
[GCC 7.2.0] on linux2

In[2]: import matplotlib.pyplot as plt
  ...: import numpy as np
  ...: 
  ...: plt.ion()
  ...: plt.rcParams['text.usetex'] = True
  ...: 
  ...: x = np.linspace(-10,10,1000) * 2*np.pi
  ...: y = np.sin(x) * 10**(-10)
  ...: 
  ...: plt.plot(x,y)
  ...: plt.savefig('/home/proxauf/test_unicode1.png')
  ...: plt.close()
  ...: 
  ...: plt.rcParams['text.usetex'] = False
  ...: 
  ...: print "plot 1 successful"
  ...: 

输出1:

Backend Qt5Agg is interactive backend. Turning interactive mode on.
plot 1 successful
Traceback (most recent call last):
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 519, in _draw_idle
    self.draw()
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 433, in draw
    self.figure.draw(self.renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/figure.py", line 1475, in draw
    renderer, self, artists, self.suppressComposite)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2607, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axis.py", line 1192, in draw
    renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/axis.py", line 1130, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/text.py", line 922, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 232, in get_text_width_height_descent
    s, fontsize, renderer=self)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 501, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 361, in make_dvi
    texfile = self.make_tex(tex, fontsize)
  File "/scratch/seismo/proxauf/conda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 259, in make_tex
    fh.write(s.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2212' in position 230: ordinal not in range(128)

输入2创建无错误的绘图:

In[3]: plt.rcParams['text.usetex'] = True
  ...: 
  ...: plt.plot(x,y)
  ...: plt.savefig('/home/proxauf/test_unicode2.png')
  ...: plt.close()
  ...: 
  ...: print "plot 2 successful"
  ...: 

输出2:

plot 2 successful

我一点也不明白。在这两种情况下,绘图都是按原样创建的(使用Latex模式,请参见下文),但是使用Input 1时,python在重置matplotlib rcParams时会出现问题。然而,当text.usetex = False也从一开始时,以非乳胶格式创建绘图就不会有问题。这里发生了什么?有可能的解决办法吗?这个错误并不是一个真正的问题(程序运行良好),而是一个我想摆脱的麻烦。你知道吗

Plot 1 (identical to plot 2)

更新:错误似乎只在从text.usetex = True切换到text.usetex = False时出现(在一个代码执行内),并且只在axes.unicode_minus = True时出现。其他方向或组合工作没有问题。你知道吗

更新2:我在PyCharm之外使用iPython 5.8时再现了错误。你知道吗


Tags: inpymatplotliblibpackageslinesiteplt