LAWU

主页:http://astroman.lamost.org

python升级后的一个小bug解决

上周末闲的无聊,把ubuntu升级到11.04,强烈不建议大家这样玩,浪费时间浪费精力得不偿失啊,这都是题外话。

升级后,系统自带python变成了2.7版的,本以为一切都可以正常,没想到一个小程序暴露出了问题。先看下面的程序(你不要管它在算什么):

import numpy as np
import matplotlib.pylab as pl
T_r0 = 2.73 # K
def T_r(z):
T_rz = T_r0*(1+z)
return T_rz
def T_k(z):
T_kz = (1+z)**2
return T_kz
def T_s(z):
T_sz = 2./(1./T_r(z)+1./T_k(z))
return T_sz
output = open(‘T.dat’,’w’)
#T = [0. for x in range(290)]
for z in range(10,300):
print >> output, T_s(z), z
output.close()
data = np.loadtxt(‘T.dat’)
x = data[:,1]
y = data[:,0]
pl.semilogx(x, y)
pl.show()

程序没问题吧?于是运行,于是出错:

$ python T_s.py

Exception in Tkinter callback

Traceback (most recent call last):

File “/usr/lib/python2.7/lib-tk/Tkinter.py”, line 1413, in __call__    return self.func(*args)

File “/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py”, line 212, in resize    self.show()

File “/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py”, line 215, in draw    FigureCanvasAgg.draw(self)

File “/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py”, line 314, in draw    self.figure.draw(self.renderer)

File “/usr/lib/pymodules/python2.7/matplotlib/artist.py”, line 46, in draw_wrapper    draw(artist, renderer, *args, **kwargs)

File “/usr/lib/pymodules/python2.7/matplotlib/figure.py”, line 773, in draw    for a in self.axes: a.draw(renderer)

File “/usr/lib/pymodules/python2.7/matplotlib/artist.py”, line 46, in draw_wrapper    draw(artist, renderer, *args, **kwargs)

File “/usr/lib/pymodules/python2.7/matplotlib/axes.py”, line 1735, in draw    a.draw(renderer)  File “/usr/lib/pymodules/python2.7/matplotlib/artist.py”, line 46, in draw_wrapper    draw(artist, renderer, *args, **kwargs)

File “/usr/lib/pymodules/python2.7/matplotlib/axis.py”, line 742, in draw    tick.draw(renderer)  File “/usr/lib/pymodules/python2.7/matplotlib/artist.py”, line 46, in draw_wrapper    draw(artist, renderer, *args, **kwargs)

File “/usr/lib/pymodules/python2.7/matplotlib/axis.py”, line 196, in draw    self.label1.draw(renderer)

File “/usr/lib/pymodules/python2.7/matplotlib/text.py”, line 518, in draw    bbox, info = self._get_layout(renderer)

File “/usr/lib/pymodules/python2.7/matplotlib/text.py”, line 280, in _get_layout    clean_line, self._fontproperties, ismath=ismath)

File “/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py”, line 156, in get_text_width_height_descent    self.mathtext_parser.parse(s, self.dpi, prop)

File “/usr/lib/pymodules/python2.7/matplotlib/mathtext.py”, line 2797, in parse    font_output = fontset_class(prop, backend)

File “/usr/lib/pymodules/python2.7/matplotlib/mathtext.py”, line 658, in __init__    self._stix_fallback = StixFonts(*args, **kwargs)

File “/usr/lib/pymodules/python2.7/matplotlib/mathtext.py”, line 900, in __init__    fullpath = findfont(name)

File “/usr/lib/pymodules/python2.7/matplotlib/font_manager.py”, line 1306, in findfont    if not os.path.exists(font):

File “/usr/lib/python2.7/genericpath.py”, line 18, in exists    os.stat(path)

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

但如果将程序中的pl.loglog或pl.semilogx(y)换成pl.plot即可完美运行。也就是在调用matplotlib库的时候出了问题。问题貌似是有点冲突,没错!因为是从python2.6升级至2.7(如果系统是完全重装的应该无此问题),2.6中的matplotlib的setting没有删除,导致该问题的出现。

解决办法很简单,把matplotlib中的fontList.cache改名或是直接删除即可:

rm ~/.matplotlib/fontList.cache

或是

mv .matplotlib/fontList.cache .matplotlib/fontList.cache.cp

Done!

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

de.arxiv.org/pdf/1106.4726v1

de.arxiv.org/pdf/1106.4726v1.

porno izle