补充@arjenve 的回答。要绘制一个 Unicode 字符,首先,找到包含该字符的字体,其次,使用该字体在 Matplotlib 中绘制字符
查找包含该字符的字体
根据this post,我们可以使用fontTools包来查找哪个字体包含我们要绘制的字符。
from fontTools.ttLib import TTFont
import matplotlib.font_manager as mfm
def char_in_font(unicode_char, font):
for cmap in font['cmap'].tables:
if cmap.isUnicode():
if ord(unicode_char) in cmap.cmap:
return True
return False
uni_char = u"✹"
# or uni_char = u"\u2739"
font_info = [(f.fname, f.name) for f in mfm.fontManager.ttflist]
for i, font in enumerate(font_info):
if char_in_font(uni_char, TTFont(font[0])):
print(font[0], font[1])
此脚本将打印字体路径和字体名称列表(所有这些字体都支持该 Unicode 字符)。示例输出如下所示
然后,我们可以使用下面的脚本来绘制这个角色(见下图)
import matplotlib.pyplot as plt
import matplotlib.font_manager as mfm
font_path = '/usr/share/fonts/gnu-free/FreeSerif.ttf'
prop = mfm.FontProperties(fname=font_path)
plt.text(0.5, 0.5, s=uni_char, fontproperties=prop, fontsize=20)
plt.show()