Unicode error using matplotlib with log scale on Windows
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()
开发者_如何学运维
I get this error (at the plt.show()
line):
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.
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.
Hope that helps!
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.
I had the same problem today, and I found the issue in github
https://github.com/matplotlib/matplotlib/issues/198
The proposed workaround is to delete the .matplotlib/fontList.cache
file, and worked for me.
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).
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.
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.
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
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!)
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).
Not a real "fix", but got my script working with minimal tinkering/downtime.
精彩评论