问题描述
有没有一种方法可以使Matplotlib呈现重音字符(é,ã,â等)?
Is there a way to get Matplotlib to render accented chars (é,ã,â,etc)?
例如,我尝试在set_yticklabels()
上使用带重音的字符,而Matplotlib而是渲染正方形,而当我使用unicode()
时,它将渲染错误的字符.
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.
有没有办法使这项工作可行?
Is there a way to make this work?
事实证明您可以使用uéã",但首先必须设置文件编码:
It turns out you can use u"éã", but first you have to set the file encoding:
# Using the magic encoding
# -*- coding: utf-8 -*-
在Matplotlib正确渲染之后
After that Matplotlib correctly renders
u"é"
我还了解到您可以使用
import matplotlib.font_manager as fm
fp1=fm.FontProperties(fname="/path/to/somefont.ttf")
ax.title("é",fontproperties=fp1)
以防您需要渲染Matplotlib没有的字符.
in case you need to render a characters that Matplotlib does not have.
推荐答案
使用u
前缀字符串以告诉Python它们是Unicode字符串:
Prefix the strings with u
to tell Python that they are Unicode strings:
ax.set_yticklabels([u'é', u'ã', u'â'])
这篇关于Matplotlib中的重音字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!