新文章 网摘 文章 随笔 日记

Python无法绘制汉字,错误RuntimeWarning: Glyph xxxxx missing from current font的产生原因解析,检测当前字体是否包含某字符

import numpy as np
import matplotlib. pyplot as plt
from matplotlib.font_manager import FontProperties

#由于默认无法正确绘制汉字,因此需指定字体路径
font_path=r"c:\windows\fonts\simsun.ttc"
font = FontProperties(fname=font_path, size=10) 

#计算正弦和余弦曲线上的点x 和 y的坐标
x= np. arange(0,  3  *np. pi, 0.1)
y_sin=np. sin(x)
y_cos=np. cos(x)
y_tan=np. tan(x)
#建立 subplot网格,高为2、宽为2
#激活第一个subplot
plt. subplot(2,  2,  1)
#绘制第一个图形
plt. plot(x, y_sin,'ob')
plt. title('正弦曲线', fontproperties=font)
#将第二个 subplot 激活,并绘制第二个图形
plt. subplot(2,  2,  2)
plt. plot(x, y_cos,'*m')
plt.title('余弦曲线', fontproperties=font)
#将第三个subplot 激活,并绘制第三个图形
plt. subplot(223)
plt. plot(x, y_tan,':r')
plt. title('正切曲线', fontproperties=font)
#展示图像
plt.show()

 

posted @ 2023-07-30 09:22  岭南春  阅读(1843)  评论(0)    收藏  举报