python Matplotlib 中的重音字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2406700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accented characters in Matplotlib
提问by OldJim
Is there a way to get Matplotlib to render accented chars (é,?,a,etc)?
有没有办法让 Matplotlib 渲染重音字符(é、?、a 等)?
For instance, I'm trying to use accented characters on set_yticklabels()
and Matplotlib renders squares instead, and when I use unicode()
it renders the wrong characters.
例如,我尝试使用重音字符,set_yticklabels()
而 Matplotlib 会渲染正方形,而当我使用unicode()
它时会渲染错误的字符。
Is there a way to make this work?
有没有办法使这项工作?
It turns out you can use u"é?", but first you have to set the file encoding:
原来你可以使用 u"é?",但首先你必须设置文件编码:
# Using the magic encoding
# -*- coding: utf-8 -*-
After that Matplotlib correctly renders
之后 Matplotlib 正确呈现
u"é"
I also learned that you can use
我还了解到你可以使用
import matplotlib.font_manager as fm
fp1=fm.FontProperties(fname="/path/to/somefont.ttf")
ax.title("é",fontproperties=fp1)
in case you need to render a characters that Matplotlib does not have.
如果您需要渲染 Matplotlib 没有的字符。
采纳答案by ptomato
Prefix the strings with u
to tell Python that they are Unicode strings:
给字符串添加前缀u
以告诉 Python 它们是 Unicode 字符串:
ax.set_yticklabels([u'é', u'?', u'a'])
回答by Steve Tjoa
Sure. You can use TeX:
当然。您可以使用 TeX:
from matplotlib import rcParams
rcParams['text.usetex'] = True
ax = ... # Axes object
ax.set_yticklabels(['$\'{e}$', '$\tilde{a}$', '$\hat{a}$'])
回答by simoes
I also had this problem specifically when I was trying to use the annotate function. Here was my error message:
当我尝试使用 annotate 函数时,我也遇到了这个问题。这是我的错误信息:
ValueError: matplotlib display text must have all code points < 128 or use Unicode strings
And here's what I used to resolve this:
这是我用来解决这个问题的:
"accented string i.e. sāo paulo".decode('utf-8')
回答by dk_sasaki
from matplotlib import rc
从 matplotlib 导入 rc
rcParams['text.latex.unicode']=True