Old*_*Jim 20 python unicode matplotlib
有没有办法让Matplotlib渲染重音字符(é,ã,â等)?
例如,我正在尝试使用重音字符set_yticklabels()而Matplotlib则渲染方块,而当我使用unicode()它时会渲染错误的字符.
有没有办法让这项工作?
事实证明你可以使用你的"éã",但首先你必须设置文件编码:
# Using the magic encoding
# -*- coding: utf-8 -*-
之后,Matplotlib正确呈现
u"é"
我还了解到你可以使用
import matplotlib.font_manager as fm
fp1=fm.FontProperties(fname="/path/to/somefont.ttf")
ax.title("é",fontproperties=fp1)
如果你需要渲染Matplotlib没有的字符.
pto*_*ato 15
使用前缀u来告诉Python它们是Unicode字符串:
ax.set_yticklabels([u'é', u'ã', u'â'])
当然.你可以使用TeX:
from matplotlib import rcParams
rcParams['text.usetex'] = True
ax = ... # Axes object
ax.set_yticklabels(['$\'{e}$', '$\tilde{a}$', '$\hat{a}$'])
当我尝试使用annotate函数时,我也特别遇到了这个问题.这是我的错误消息:
ValueError: matplotlib display text must have all code points < 128 or use Unicode strings
以下是我用来解决这个问题的方法:
"accented string i.e. s?o paulo".decode('utf-8')