gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
在使用python,matplotlib绘图时报错Glyph 21482 (\N{CJK UNIFIED IDEOGRAPH-53EA}) missing from…
- User
在使用python,matplotlib绘图时报错Glyph 21482 (\N{CJK UNIFIED IDEOGRAPH-53EA}) missing from current font.怎么解决
- Assistant
这个错误通常是由于matplotlib默认字体不支持中文字符,可以通过以下三种方法解决:
- 安装支持中文字符的字体
可以安装一些支持中文字符的字体,如SimHei、FangSong等,然后将其设置为matplotlib的默认字体。示例代码如下:
codeimport matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为SimHei plt.rcParams['axes.unicode_minus'] = False # 解决负号'-'显示为方块的问题- 下载中文字体文件
可以去一些网站下载中文字体文件,如simsun.ttf,然后使用font_manager模块将其注册为matplotlib的字体。示例代码如下:
codeimport matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font = FontProperties(fname=r"路径/simsun.ttf", size=14) # 设置中文字体为SimSun,字号为14 plt.xlabel('横轴', fontproperties=font) plt.ylabel('纵轴', fontproperties=font) plt.title('示例图', fontproperties=font)- 使用已有字体,并添加中文字体支持