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

时间:2021-04-04 23:41:13

I'm using python 2.6 and matplotlib. If I run the sample histogram_demo.py provided in the matplotlib gallery page, it works fine. I've simplified this script greatly:

我正在使用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()

I get this error (at the plt.show() line):

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

TypeError: coercing to Unicode: need string or buffer, dict found

I've tried changing the backend to many different values - nothing helps. I am using Qt4Agg. Is this a font issue? It seems that it must be something with my configuration. Note: Because of other problems, I just installed a fresh copy of python26, matplotlib, numpy, scipy. I have another XP-box running python26 and it executes both versions of the script with no errors. I hope someone can help. Many thanks in advance.

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

5 个解决方案

#1


8  

This is a bug in the font management of matplotlib, on my machine this is the file /usr/lib/pymodules/python2.6/matplotlib/font_manager.py:1220. I've highlighted the change in the code snippet below; this is fixed in the newest version of matplotlib.

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

This error occurs only if no "good" font was found and the font manager falls back to a default font. Therefore the error occured without apparent reason, probably because of changes in the installed fonts.

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

Hope that helps!

希望有所帮助!

#2


5  

I had the same problem with matplotlib 0.98.5.2. I was able to fix it by upgrading to matplotlib 1.0.1 (0.99.3 didn't work), or by blowing away my ~/.matplotlib directory. Not sure what the equivalent is for Windows.

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

#3


1  

I had the same problem today, and I found the issue in github

我今天遇到了同样的问题,我在github中发现了这个问题

https://github.com/matplotlib/matplotlib/issues/198

The proposed workaround is to delete the .matplotlib/fontList.cache file, and worked for me.

建议的解决方法是删除.matplotlib / fontList.cache文件,并为我工作。

#4


0  

I experienced a similar error today, concerning code that I know for a fact was working a week ago. I also have recently uninstalled/reinstalled both Matplotlib and Numpy, while checking something else (I'm using Python 2.5).

我今天遇到了类似的错误,关于一个星期前我所知道的事实。我最近也卸载/重新安装了Matplotlib和Numpy,同时检查其他东西(我正在使用Python 2.5)。

The code went something like this:

代码是这样的:

self.ax.cla()
if self.logy: self.ax.set_yscale('log')
self.canvas.draw()

Whenever it was run with self.logy as True, it failed as above. Otherwise, it worked perfectly fine.

每当它以self.logy运行为True时,它就会失败,如上所述。否则,它工作得非常好。

I ended up sidsteping the issue by uninstalling Matplotlib and Numpy and installing the latest versions of them. However, the version throwing the error had previously been used with no problems. Only after swapping the old version for the newer one and back again did this start happening.

我最终解决了这个问题,卸载了Matplotlib和Numpy并安装了它们的最新版本。但是,抛出错误的版本以前一直没有问题。只有在将旧版本换成新版本并再次返回之后才开始发生这种情况。

Perhaps the uninstall/reinstall process messes up certain aspects of the configuration files.

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

For completeness, here is the complete traceback given:

为了完整起见,这里给出了完整的回溯:

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

#5


0  

Thanks for explaining the issue!

谢谢你解释这个问题!

Since I'm using the Mac OS 10.6 system install of matplotlib, (and I'm stuck on Python2.5 due to other package requirements) I am not interested in upgrading matplotlib (I just can't handle all the versioning of open-source packages!)

由于我使用的是matplotlib的Mac OS 10.6系统安装,(由于其他软件包的要求,我被困在Python2.5上)我对升级matplotlib不感兴趣(我只是无法处理所有版本的open-源包!)

So the fix I randomly tried, which worked, was to edit my ~/.matplotlib/matplotlibrc and enable LaTex, by setting text.usetex : True (since it showed a long list of supported fonts, so I thought it might try harder to find a "good" font).

所以我随机尝试的修复程序是编辑我的〜/ .matplotlib / matplotlibrc并启用LaTex,通过设置text.usetex:True(因为它显示了一长串支持的字体,所以我认为它可能会更加努力找到一个“好”的字体)。

Not a real "fix", but got my script working with minimal tinkering/downtime.

不是一个真正的“修复”,但让我的脚本工作最小的修补/停机时间。

#1


8  

This is a bug in the font management of matplotlib, on my machine this is the file /usr/lib/pymodules/python2.6/matplotlib/font_manager.py:1220. I've highlighted the change in the code snippet below; this is fixed in the newest version of matplotlib.

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

This error occurs only if no "good" font was found and the font manager falls back to a default font. Therefore the error occured without apparent reason, probably because of changes in the installed fonts.

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

Hope that helps!

希望有所帮助!

#2


5  

I had the same problem with matplotlib 0.98.5.2. I was able to fix it by upgrading to matplotlib 1.0.1 (0.99.3 didn't work), or by blowing away my ~/.matplotlib directory. Not sure what the equivalent is for Windows.

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

#3


1  

I had the same problem today, and I found the issue in github

我今天遇到了同样的问题,我在github中发现了这个问题

https://github.com/matplotlib/matplotlib/issues/198

The proposed workaround is to delete the .matplotlib/fontList.cache file, and worked for me.

建议的解决方法是删除.matplotlib / fontList.cache文件,并为我工作。

#4


0  

I experienced a similar error today, concerning code that I know for a fact was working a week ago. I also have recently uninstalled/reinstalled both Matplotlib and Numpy, while checking something else (I'm using Python 2.5).

我今天遇到了类似的错误,关于一个星期前我所知道的事实。我最近也卸载/重新安装了Matplotlib和Numpy,同时检查其他东西(我正在使用Python 2.5)。

The code went something like this:

代码是这样的:

self.ax.cla()
if self.logy: self.ax.set_yscale('log')
self.canvas.draw()

Whenever it was run with self.logy as True, it failed as above. Otherwise, it worked perfectly fine.

每当它以self.logy运行为True时,它就会失败,如上所述。否则,它工作得非常好。

I ended up sidsteping the issue by uninstalling Matplotlib and Numpy and installing the latest versions of them. However, the version throwing the error had previously been used with no problems. Only after swapping the old version for the newer one and back again did this start happening.

我最终解决了这个问题,卸载了Matplotlib和Numpy并安装了它们的最新版本。但是,抛出错误的版本以前一直没有问题。只有在将旧版本换成新版本并再次返回之后才开始发生这种情况。

Perhaps the uninstall/reinstall process messes up certain aspects of the configuration files.

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

For completeness, here is the complete traceback given:

为了完整起见,这里给出了完整的回溯:

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

#5


0  

Thanks for explaining the issue!

谢谢你解释这个问题!

Since I'm using the Mac OS 10.6 system install of matplotlib, (and I'm stuck on Python2.5 due to other package requirements) I am not interested in upgrading matplotlib (I just can't handle all the versioning of open-source packages!)

由于我使用的是matplotlib的Mac OS 10.6系统安装,(由于其他软件包的要求,我被困在Python2.5上)我对升级matplotlib不感兴趣(我只是无法处理所有版本的open-源包!)

So the fix I randomly tried, which worked, was to edit my ~/.matplotlib/matplotlibrc and enable LaTex, by setting text.usetex : True (since it showed a long list of supported fonts, so I thought it might try harder to find a "good" font).

所以我随机尝试的修复程序是编辑我的〜/ .matplotlib / matplotlibrc并启用LaTex,通过设置text.usetex:True(因为它显示了一长串支持的字体,所以我认为它可能会更加努力找到一个“好”的字体)。

Not a real "fix", but got my script working with minimal tinkering/downtime.

不是一个真正的“修复”,但让我的脚本工作最小的修补/停机时间。