C:\Program Files\JetBrains\PyCharm 2019.1.3\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from current font.
问题
中文字体显示问题
思路
中文字体显示问题
Pycharm在使用matplotlib画图时,如果在title,xlabel,ylabel中出现了中文,则会出现字体警告,中文字符显示为方框,具体如下例:
from sklearn import datasets
import matplotlib.pyplot as plt
图像数据集
china = datasets.load_sample_image(‘china.jpg’) plt.axis(‘off’) plt.title(“中国颐和园图像”) plt.imshow(china) plt.show()
运行代码,中文方框报错:
D:\Program Files\JetBrains\PyCharm 2022.1.3\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 20013 (\N{CJK UNIFIED IDEOGRAPH-4E2D}) missing from current font. FigureCanvasAgg.draw(self)
可以看到报错中“missing from current font” ,即默认的字体中不包含中文字符
解决方法
在画图代码中设置字体
from pylab import mpl # 设置中文显示字体 mpl.rcParams["font.sans-serif"] = ["SimHei"]
解决
成功运行!