Python 在Windows上使用具有日志比例的matplotlib时出现Unicode错误
我正在使用Python2.6和matplotlib。如果我运行matplotlib gallery页面中提供的示例histogram_demo.py,它可以正常工作。我已经大大简化了这个脚本:Python 在Windows上使用具有日志比例的matplotlib时出现Unicode错误,python,matplotlib,Python,Matplotlib,我正在使用Python2.6和matplotlib。如果我运行matplotlib gallery页面中提供的示例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(
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()
我尝试过将后端更改为许多不同的值-没有任何帮助。我正在使用
Qt4Agg
。这是字体问题吗?看来一定是我的配置问题。注意:由于其他问题,我刚刚安装了python26、matplotlib、numpy、scipy的新副本。我有另一个运行python26的xpbox,它执行两个版本的脚本,没有错误。我希望有人能帮忙。非常感谢。我今天遇到了一个类似的错误,关于我所知道的一周前正在运行的代码。我最近还卸载/重新安装了Matplotlib和Numpy,同时检查了其他内容(我使用的是Python 2.5)
代码是这样的:
self.ax.cla()
if self.logy: self.ax.set_yscale('log')
self.canvas.draw()
无论何时以self.logic作为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
这是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
只有在找不到“好”字体且字体管理器返回默认字体时,才会发生此错误。因此,发生错误时没有明显的原因,可能是因为安装的字体发生了更改
希望有帮助 matplotlib 0.98.5.2也有同样的问题。我可以通过升级到matplotlib 1.0.1(0.99.3不起作用)或清除~/.matplotlib目录来修复它。我不确定Windows的等价物是什么。我今天也遇到了同样的问题,我在github中发现了这个问题
建议的解决方法是删除
.matplotlib/fontList.cache
文件,并为我工作。感谢您解释此问题
由于我使用的是Mac OS 10.6系统安装的matplotlib(由于其他软件包要求,我被困在Python2.5上),所以我对升级matplotlib不感兴趣(我只是无法处理所有开源软件包的版本控制!)
因此,我随机尝试的修复方法是编辑我的~/.matplotlib/matplotlibrc
并通过设置text.usetex:True
(因为它显示了一长串支持的字体,所以我想它可能会更努力地找到一种“好”的字体)
这不是一个真正的“修复”,但使我的脚本能够以最小的修补/停机时间工作。不管它值多少钱,因为这显然取决于系统,在matplotlib用户列表中询问这个问题可能会更幸运:您能同时发布整个回溯吗?摆脱~/.matplotlib/fontList.cache为我解决了这个问题。特别是删除~/.matplotlib/fontList.cache文件。不需要删除整个文件夹
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