如何在matplotlib中使用unicode符号? [英] How to use unicode symbols in matplotlib?
问题描述
import matplotlib.pyplot as pyplot
pyplot.figure()
pyplot.xlabel(u"\u2736")
pyplot.show()
这是我可以创建的最简单的代码来显示我的问题.轴标签符号原意是六点星形,但显示为方框.如何更改它以便显示星星?我试过添加评论:
Here is the simplest code I can create to show my problem. The axis label symbol is meant to be a six-pointed star but it shows as a box. How do I change it so the star is shown? I've tried adding the comment:
#-*- coding: utf-8 -*-
就像之前建议的答案一样,但它不起作用,以及使用 matplotlib.rc
或 matplotlib.rcParams
也不起作用.将不胜感激.
like previous answers suggested but it didn't work, as well as using matplotlib.rc
or matplotlib.rcParams
which also didn't work. Help would be appreciated.
推荐答案
您需要一种具有给定 unicode 字符的字体,STIX 字体应包含星号.您需要找到或下载 STIX 字体,当然任何其他带有给定符号的 ttf 文件应该没问题.
You'll need a font that has the given unicode character, STIX fonts should contain the star symbol. You'll need to locate or download the STIX fonts, ofcourse any other ttf file with the given symbol should be fine.
import matplotlib.pyplot as pyplot
from matplotlib.font_manager import FontProperties
if __name__ == "__main__":
pyplot.figure()
prop = FontProperties()
prop.set_file('STIXGeneral.ttf')
pyplot.xlabel(u"\u2736", fontproperties=prop)
pyplot.show()
这篇关于如何在matplotlib中使用unicode符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!