Chr*_*s H 5 python unicode matplotlib
import matplotlib.pyplot as pyplot
pyplot.figure()
pyplot.xlabel(u"\u2736")
pyplot.show()
Run Code Online (Sandbox Code Playgroud)
这是我可以创建的最简单的代码来显示我的问题.轴标签符号是六角星,但它显示为一个方框.如何更改它以便显示星标?我试过添加评论:
#-*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
像以前的答案建议,但它没有用,以及使用matplotlib.rc或matplotlib.rcParams哪些也没有工作.帮助将不胜感激.
您需要一种具有给定 unicode 字符的字体,STIX字体应包含星号。您需要找到或下载 STIX 字体,当然任何其他带有给定符号的 ttf 文件应该没问题。
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()
Run Code Online (Sandbox Code Playgroud)
补充@arjenve的答案。要绘制 Unicode 字符,首先,找到包含该字符的字体,其次,使用该字体通过 Matplotlib 绘制字符
\n\n根据这篇文章,我们可以使用fontTools包来查找哪种字体包含我们想要绘制的字符。
\n\nfrom fontTools.ttLib import TTFont\nimport matplotlib.font_manager as mfm\n\ndef char_in_font(unicode_char, font):\n for cmap in font[\'cmap\'].tables:\n if cmap.isUnicode():\n if ord(unicode_char) in cmap.cmap:\n return True\n return False\n\nuni_char = u"\xe2\x9c\xb9"\n# or uni_char = u"\\u2739"\n\nfont_info = [(f.fname, f.name) for f in mfm.fontManager.ttflist]\n\nfor i, font in enumerate(font_info):\n if char_in_font(uni_char, TTFont(font[0])):\n print(font[0], font[1])\nRun Code Online (Sandbox Code Playgroud)\n\n该脚本将打印字体路径和字体名称的列表(所有这些字体都支持该 Unicode 字符)。示例输出如下所示
\n\n\n\n然后,我们可以使用以下脚本来绘制这个角色(见下图)
\n\nimport matplotlib.pyplot as plt\nimport matplotlib.font_manager as mfm\n\nfont_path = \'/usr/share/fonts/gnu-free/FreeSerif.ttf\'\nprop = mfm.FontProperties(fname=font_path)\nplt.text(0.5, 0.5, s=uni_char, fontproperties=prop, fontsize=20)\n\nplt.show()\nRun Code Online (Sandbox Code Playgroud)\n\n\n
| 归档时间: |
|
| 查看次数: |
3885 次 |
| 最近记录: |