在 Windows 上使用带有对数刻度的 matplotlib 的 Unicode 错误 [英] Unicode error using matplotlib with log scale on Windows

查看:37
本文介绍了在 Windows 上使用带有对数刻度的 matplotlib 的 Unicode 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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()

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

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

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

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

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.

推荐答案

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

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.

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.

希望有帮助!

这篇关于在 Windows 上使用带有对数刻度的 matplotlib 的 Unicode 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆