在Windows上使用带有日志比例的matplotlib的Unicode错误

cas*_*sey 5 python matplotlib

我正在使用python 2.6和matplotlib.如果我运行matplotlib库页面中提供的示例histogram_demo.py,它可以正常工作.我大大简化了这个脚本:

import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

fig = plt.figure()
ax = fig.add_subplot(111)

n, bins, patches = ax.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

ax.set_yscale('log')  # <---- add this line to generate the error
plt.show()
Run Code Online (Sandbox Code Playgroud)

我收到此错误(在该plt.show()行):

TypeError: coercing to Unicode: need string or buffer, dict found
Run Code Online (Sandbox Code Playgroud)

我已经尝试将后端更改为许多不同的值 - 没有任何帮助.我在用Qt4Agg.这是字体问题吗?似乎它必须是我的配置.注意:由于其他问题,我刚刚安装了python26,matplotlib,numpy,scipy的新副本.我有另一个运行python26的XP-box,它执行脚本的两个版本,没有错误.我希望有人能帮帮忙.提前谢谢了.

小智 8

这是matplotlib的字体管理中的一个错误,在我的机器上这是文件/usr/lib/pymodules/python2.6/matplotlib/font_manager.py:1220.我已在下面的代码段中突出显示了更改; 这是在最新版本的matplotlib中修复的.

if best_font is None or best_score >= 10.0:
    verbose.report('findfont: Could not match %s. Returning %s' %
                       (prop, self.defaultFont))
    [+]result = self.defaultFont[fontext]
    [-]result = self.defaultFont
    print "defaultFont", result
else:
    verbose.report('findfont: Matching %s to %s (%s) with score of %f' %
                       (prop, best_font.name, best_font.fname, best_score))
    result = best_font.fname
    print "best_font", result
Run Code Online (Sandbox Code Playgroud)

仅当未找到"好"字体并且字体管理器回退到默认字体时才会发生此错误.因此,错误发生没有明显的原因,可能是因为安装的字体发生了变化.

希望有所帮助!


Pau*_*ice 5

我对matplotlib 0.98.5.2也有同样的问题.我能够通过升级到matplotlib 1.0.1(0.99.3不起作用)或者通过吹掉我的〜/ .matplotlib目录来修复它.不确定Windows的等价物是什么.

  • 摆脱〜/ .matplotlib/fontList.cache为我解决了问题. (3认同)

小智 0

我今天遇到了类似的错误,涉及我知道一周前有效的代码。我最近还卸载/重新安装了 Matplotlib 和 Numpy,同时检查其他内容(我正在使用 Python 2.5)。

代码是这样的:

self.ax.cla()
if self.logy: self.ax.set_yscale('log')
self.canvas.draw()
Run Code Online (Sandbox Code Playgroud)

每当以 self.logy 为 True 运行它时,它就会像上面那样失败。否则,它工作得很好。

我最终通过卸载 Matplotlib 和 Numpy 并安装它们的最新版本来回避这个问题。然而,引发错误的版本以前使用过,没有任何问题。只有在将旧版本替换为新版本并再次返回后,这种情况才开始发生。

也许卸载/重新安装过程会弄乱配置文件的某些方面。

为了完整起见,以下是给出的完整回溯:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\path\to\file\being\called\by\Tkinter.py", line 1081, in refresh
    self.canvas.draw()
  File "C:\Python25\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 215, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python25\Lib\site-packages\matplotlib\backends\backend_agg.py", line 314, in draw
    self.figure.draw(self.renderer)
  File "C:\Python25\Lib\site-packages\matplotlib\artist.py", line 46, in draw_wrapper
    draw(artist, renderer, *kl)
  File "C:\Python25\Lib\site-packages\matplotlib\figure.py", line 773, in draw
    for a in self.axes: a.draw(renderer)
  File "C:\Python25\Lib\site-packages\matplotlib\artist.py", line 46, in draw_wrapper
    draw(artist, renderer, *kl)
  File "C:\Python25\Lib\site-packages\matplotlib\axes.py", line 1735, in draw
    a.draw(renderer)
  File "C:\Python25\Lib\site-packages\matplotlib\artist.py", line 46, in draw_wrapper
    draw(artist, renderer, *kl)
  File "C:\Python25\Lib\site-packages\matplotlib\axis.py", line 742, in draw
    tick.draw(renderer)
  File "C:\Python25\Lib\site-packages\matplotlib\artist.py", line 46, in draw_wrapper
    draw(artist, renderer, *kl)
  File "C:\Python25\Lib\site-packages\matplotlib\axis.py", line 196, in draw
    self.label1.draw(renderer)
  File "C:\Python25\Lib\site-packages\matplotlib\text.py", line 515, in draw
    bbox, info = self._get_layout(renderer)
  File "C:\Python25\Lib\site-packages\matplotlib\text.py", line 279, in _get_layout
    clean_line, self._fontproperties, ismath=ismath)
  File "C:\Python25\Lib\site-packages\matplotlib\backends\backend_agg.py", line 156, in get_text_width_height_descent
    self.mathtext_parser.parse(s, self.dpi, prop)
  File "C:\Python25\Lib\site-packages\matplotlib\mathtext.py", line 2797, in parse
    font_output = fontset_class(prop, backend)
  File "C:\Python25\Lib\site-packages\matplotlib\mathtext.py", line 658, in __init__
    self._stix_fallback = StixFonts(*args, **kwargs)
  File "C:\Python25\Lib\site-packages\matplotlib\mathtext.py", line 900, in __init__
    fullpath = findfont(name)
  File "C:\Python25\Lib\site-packages\matplotlib\font_manager.py", line 1306, in findfont
    if not os.path.exists(font):
  File "C:\Python25\lib\ntpath.py", line 255, in exists
    st = os.stat(path)
TypeError: coercing to Unicode: need string or buffer, dict found
Run Code Online (Sandbox Code Playgroud)